Re: [SMARTY-DEV] mixing of asign() and assign_by_ref() overwrites external vars
| From: | boots | Date: | Mon, 07 Jun 2004 21:09:46 +0000 |
| Subject: | Re: [SMARTY-DEV] mixing of asign() and assign_by_ref() overwrites external vars | ||
| References: | 1 | Groups: | php.smarty.dev |
| Request: | Send a blank email to php-smarty-dev+get-2407@lists.php.net to get a copy of this message | ||
--- Sebastian Mendel <lists@sebastianmendel.de> wrote:
> Boots schrieb:
>
> > [...]
> > It doesn't solve the problem: the original poster was mis-using the
> > feature.
>
> :-(
>
> i dont think so!
[..snip..]
>
> $myvar = true;
>
> var_dump($myvar);
> echo '<br />';
> $test = new Smarty;
> $test->assign_by_ref('myvar', $myvar);
> $test_2 = $test;
> $test_2->assign('myvar', false);
>
> var_dump($myvar);
> echo '<br />';
I'm suggesting that you don't really want to use assign_by_ref. How is
this example any different than:
$mvar = true;
var_dump($mvar);
$mvar2 =& $mvar;
$mvar2 = false;
var_dump($mvar);
The whole point of assigning-by-reference is that you are aliasing the
variable as opposed to making a copy. When you assign $test_2 = $test,
even though you don't do it by-reference, you are making a copy of an
object that contains references. When you alter the reference (even in
the copy), you necessarily alter the original var. This is the way PHP
works and I don't think it implies any problems with the Smarty
semantics.
If you don't want that behaviour, don't use assign_by_ref. If you still
want to assign_by_ref, then work around PHP's behaviour in the usual
ways:
$myvar = true;
var_dump($myvar);
echo '<br />';
$test = new Smarty;
$test->assign_by_ref('myvar', $myvar);
$test_2 = $test;
// following is equivalent to unset($test_2->_tpl_vars['myvar'];
$test_2->clear_assign('myvar');
$test_2->assign('myvar', false);
var_dump($myvar);
echo '<br />';
xo boots
__________________________________
Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/