php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23705 Reassigned object-ref params don't propagate back properly.
Submitted: 2003-05-19 14:59 UTC Modified: 2003-08-05 10:23 UTC
From: joachim at lous dot org Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.0b2-dev, 4.3.3RC2-dev OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: joachim at lous dot org
New email:
PHP Version: OS:

 

 [2003-05-19 14:59 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-13 00:14 UTC] sniper@php.net
Same result with ZE2.

 [2003-08-04 03:32 UTC] 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-08-05 10:23 UTC] sniper@php.net
Not bug -> bogus.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 14:01:33 2024 UTC