#28359 [Csd->WFx]: serialize() combined with references and objects modifies $this

From: Date: Mon, 05 Jul 2004 09:26:00 +0000
Subject: #28359 [Csd->WFx]: serialize() combined with references and objects modifies $this
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-61686@lists.php.net to get a copy of this message
ID: 28359 Updated by: moriyoshi@php.net Reported By: mira at st dot jyu dot fi -Status: Closed +Status: Wont fix Bug Type: Class/Object related Operating System: Linux Mandrake 10.0 PHP Version: 4.3.4 New Comment: I mistook this as a PHP5 problem. This issue has been addressed in PHP5. You don't return a scalar value that is not a variable as reference. OK: function &foo() { return $a; } NG: function &bar() { return $a = %b; } function &baz() { return @$a; } Previous Comments: ------------------------------------------------------------------------ [2004-07-05 11:21:23] moriyoshi@php.net This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. ------------------------------------------------------------------------ [2004-05-11 14:23:12] mira at st dot jyu dot fi Description: ------------ See the attached script. The problem is reproduceable with PHP 4.3.4 on Apache 2.0.48 (Apache and PHP versions that are distributed with Mandrake 10.0). The problem seems to be caused by method setX(). If I replace that method with function setX($x) { return $this->x = $x; } or function &setX($x) { $this->x = $x; return $this->x; } I get the expected end result. [Note that the former doesn't return the reference so it isn't equivalent to original code.] Also note that if I remove the first echo, the result of the second echo is different so it seems that serialize($this) is modifying $this. It might be that object's internal represenation is already trashed, though. Reproduce code: --------------- <pre><?php class foo { function test() { $this->setX("28"); echo "this = ".serialize($this)."\n"; echo "this->getX() = ".serialize($this->getX())."\n"; } function &setX($x) { return $this->x = $x; } function &getX() { return $this->x; } } $foo =& new foo(); $foo->test(); ?></pre> Expected result: ---------------- this = O:3:"foo":1:{s:1:"x";s:2:"28";} this->getX() = s:2:"28"; Actual result: -------------- this = O:3:"foo":1:{s:1:"x";O:3:"foo":1:{s:1:"x";N;}} this->getX() = O:3:"foo":1:{s:1:"x";O:3:"foo":1:{s:1:"x";R:2;}} ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=28359&edit=1

« previous php.bugs (#61686) next »