Bug #3373: Object passed by reference don't appear to work

From: Date: Tue, 01 Feb 2000 14:30:39 +0000
Subject: Bug #3373: Object passed by reference don't appear to work
Groups: php.dev 
Request: Send a blank email to php-dev+get-15107@lists.php.net to get a copy of this message
From: john.martin@usa.com Operating system: NT PHP version: 3.0.13 PHP Bug Type: Other Bug description: Object passed by reference don't appear to work There appears to be an odd behavior when it comes to passing objects by reference. Instead of passing the address of the object, it passes a copy of the object. This is unless I'm doing something wrong (not me #:-) )? Note that the following code has class that contains a second class. There is a method on the first class (foo) to return the reference of the object (bar) contained within it. You can get the reference to the object okay and the attributes of the object (bar) contains the correct information. If you call a modifier method on the 2nd reference to the object bar, it only updates that reference and not the object contained in the parent object foo. <pre> <?php // test.php class foo { var $x; var $bar; function foo($x=0, $y=0) { $this->setX($x); $this->bar = new bar($y); } function setX($val) { $this->x = $val; } function getX() { return $this->x; } function getY() { return $this->bar->getY(); } function getBar(&$objref) { $objref = $this->bar; } } class bar { var $y; function bar($y) { $this->setY($y); } function setY($y) { $this->y = $y; } function getY() { return $this->y; } } $a = new foo(12,33); print "<b>Initial values:</b><br>"; printf("a = [%d,%d]<br>\n", $a->getX(), $a->getY()); print "<b>Updated values:</b><br>"; $a->setx(234); printf("a = [%d,%d]<br>\n", $a->getX(), $a->getY()); print "<b>Updating 2nd Reference of Object (y=7777):</b><br>"; // Get the reference to object bar. $a->getBar($b); $b->setY(7777); printf("b = [%d]<br>\n", $b->getY()); printf("a = [%d,%d]<br>\n", $a->getX(), $a->getY()); // Note that a->y->y is not modified. ?> </pre>

« previous php.dev (#15107) next »