Re[4]: mixing of asign() and assign_by_ref() overwrites external vars

Dmitry Koteroff <[email protected]>
Newsgroups gmane.comp.php.smarty.devel
Message-ID <[email protected]>
B> 1) I don't agree that it is unsuspected; it follows the PHP4 paradigm.
Do, $smarty->assign("a", "b") has EQUAL paradigm as PHP's $a="b".
Agree?  Do  you  plan  exactly  this  while  developing  assign()
function? Is it documented?

B> 2) I don't agree that it is transparent: messju already pointed out the
B> problem with deprecated pass-by-reference.
It is not a problem. You may not use deprecated syntax at all.

B> 3) I agree with Monte's sentiment: don't fix what isn't broken. In my
B> last post, I show the standard work-around for getting the behaviour
B> that was desired in the case originally given. It uses an existing API
B> call which merely wraps what you would have to do with straight PHP:
B> use unset().
Oh.

B> 4) You are overloading the meaning of assign() to allow
B> pass-by-reference whereas the Smarty API already decided long ago to
B> break that into a different API call, namely assign_by_ref(). I think
B> it is more confusing when you have two methods to do ostensibly the
B> same thing.
But you surely can not modify API nor use deprecated syntax.

    function assign($tpl_var, $val = null)
    {
        // First copy - always!
        $value = $val;
        // Then - assign by ref, to avoid unexpected (?) behavour.
        if (is_array($tpl_var)){
            foreach ($tpl_var as $key => $val) {
                if ($key != '') {
                    $this->_tpl_vars[$key] =& $val;
                }
            }
        } else {
            if ($tpl_var != '')
                $this->_tpl_vars[$tpl_var] =& $value;
        }
    }

So, now API is not touched, deprecated & call works as without &,
but  assign()  after  assign_by_ref()  does  not  modify  source
variable.

P.S.
It is now question of principle, of course, not practical.

-- 
Best regards,
  Dmitry Koteroff.

-- 
Smarty Development Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.