#48483 [Opn->Bgs]: this pointer is not passed by reference correctly

From: Date: Tue, 09 Jun 2009 02:11:44 +0000
Subject: #48483 [Opn->Bgs]: this pointer is not passed by reference correctly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-138123@lists.php.net to get a copy of this message
ID: 48483 Updated by: scottmac@php.net Reported By: chase at circleofmoms dot com -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: ubuntu 8.04 PHP Version: 5.2.9 New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php You can't re-assign $this, ever. It prints an error normally but when references are used it can't be properly detected. Try the following and see the error. <?php class Thing { var $data; public function updateData($newData) { $this = new Thing(); } } $thing = new Thing(); $thing->data = "foo"; $thing->updateData("bar"); Previous Comments: ------------------------------------------------------------------------ [2009-06-06 00:39:04] chase at circleofmoms dot com Description: ------------ When you pass the $this pointer by reference and change data for the object it points to, that data is not correctly updated. Reproduce code: --------------- function changeAddress(&$oldThing) { $oldThing = new Thing(); } class Thing { var $data; public function updateData($newData) { ThingChanger::changeData($this,$newData); var_dump($this); var_dump($this->data); } } class ThingChanger { public static function changeData(&$thingToChange,$newData) { changeAddress($thingToChange); $thingToChange->data = $newData; } } $thing = new Thing(); $thing->data = "foo"; $thing->updateData("bar"); Expected result: ---------------- object(Thing)#2 (1) { ["data"]=> string(3) "bar" } string(3) "bar" Actual result: -------------- object(Thing)#2 (1) { ["data"]=> string(3) "bar" } string(3) "foo" ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=48483&edit=1

« previous php.bugs (#138123) next »