#45335 [Opn]: By-value assignment is ignored if that variable contains a reference

From: Date: Mon, 14 Jul 2008 23:58:53 +0000
Subject: #45335 [Opn]: By-value assignment is ignored if that variable contains a reference
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-951@lists.php.net to get a copy of this message
ID: 45335 Updated by: jani@php.net Reported By: kosthorst at apobyte dot de Status: Open -Bug Type: Scripting Engine problem +Bug Type: Documentation problem Operating System: Debian Etch PHP Version: 5.2.6 New Comment: This special case propably should have an example and explanation itself in the docs. References in PHP are "only" aliases. Doing $a =& $b means that after that $a and $b are referring to same content [1]. If you don't unset() [2] the reference, they're still referring to same content. So doing $a = $b would be basically same as doing $b = $b.. [1] http://www.php.net/manual/en/language.references.whatdo.php [2] http://www.php.net/manual/en/language.references.unset.php Previous Comments: ------------------------------------------------------------------------ [2008-06-23 13:00:52] kosthorst at apobyte dot de Description: ------------ I assign something by-reference to a variable. If I assign the same stuff again to the same variable by-value, this assignment is ignored. In the reproduce code, $bar first is a reference to $foo. $bar is modified via array_pop(). This modifies $foo, too. Then, $foo is assigned to $bar by-value. Again, $bar is modified via array_pop(). And again, $foo is modified too, which should not happen, because $bar should not be a reference to $foo any more. An unset($bar); right before the second assignment fixes the issue. Reproduce code: --------------- $foo = array(1, 2, 3, 4, 5); $bar =& $foo; array_pop($bar); var_dump($foo); $bar = $foo; array_pop($bar); var_dump($foo); Expected result: ---------------- array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } Actual result: -------------- array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45335&edit=1

« previous php.doc.bugs (#951) next »