#23705 [Csd->Bgs]: Reassigned object-ref params don't propagate back properly.
| From: | sniper@php.net | Date: | Tue, 05 Aug 2003 15:23:20 +0000 |
| Subject: | #23705 [Csd->Bgs]: Reassigned object-ref params don't propagate back properly. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-45383@lists.php.net to get a copy of this message | ||
ID: 23705
Updated by: sniper@php.net
Reported By: joachim at lous dot org
-Status: Closed
+Status: Bogus
Bug Type: Zend Engine 2 problem
Operating System: *
PHP Version: 5.0.0b2-dev, 4.3.3RC2-dev
New Comment:
Not bug -> bogus.
Previous Comments:
------------------------------------------------------------------------
[2003-08-04 03:32:02] zeev@php.net
It's not a bug.
Assigning by reference breaks any previous references that may have
existed. So the implementation of replace() won't really work, as
$dest ends up being a referenec to $src, but only locally in the
replace() function, without effecting the global $dest.
I can't think of an easy way of achieving replace() right now, but
either way, the behavior you're experiencing is intended.
FWIW, in PHP 5, it would be practical to implement replace() with
simple assignment (=) instead of assign-by-ref (=&), if you're dealing
with objects.
------------------------------------------------------------------------
[2003-07-13 00:14:58] sniper@php.net
Same result with ZE2.
------------------------------------------------------------------------
[2003-05-19 14:59:47] joachim at lous dot org
It doesn't seem to be possible to replace a passed object references
with a different one.
I expected the following code to print "5":
class a{
var $val;
function a($val){ $this->val = $val;}
}
function replace(&$src, &$dest){
$dest =& $src;
}
$src = new a(2);
$dest = new a(1);
replace($src,$dest);
$src->val=5;
echo $dest->val;
In stead it prints "1".
Changing the '=&' to '=', it prints '2', as expected.
Surely this must be a bug? If not, is there really no way to get the
reference across?
I need this for building trees: passing the current node pointer, have
the function change the tree and make the ref point to a new current
node. I can work around it by returning the new reference in stead, but
that only works for one ref.
Using binary installer from website.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=23705&edit=1