Bug #12775 Updated: Incorrect result using references and objects

From: Date: Wed, 15 Aug 2001 22:11:10 +0000
Subject: Bug #12775 Updated: Incorrect result using references and objects
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-63346@lists.php.net to get a copy of this message
ID: 12775 Updated by: jeroen Reported By: wayne@webmotif.com Old Status: Open Status: Suspended Bug Type: Scripting Engine problem Operating System: All PHP Version: 4.0.6 New Comment: Shallow copying of objects and arrays bit you... Will be fixed in PHP 5 Suspended (it could be called a 'feature' though...) Previous Comments: ------------------------------------------------------------------------ [2001-08-15 18:00:05] wayne@webmotif.com If you set a property of an object to a global variable, it is no longer possible to make a copy of that object! Every "copy" becomes a reference. Example: --/ snip /-- class foo { function foo() { $this->test = 'Test'; $GLOBALS['test'] =& $this->test; } } $obj1 = new foo(); $copyOfObj1 = $obj1; // Should be a copy, but is reference $obj1->test = "changed!"; echo $copyOfObj1->test.'<br>'; --/ snip /-- The result of this code is: "changed!" but it should be "Test". $copyOfObj1 should be a copy of $obj1, but instead it acts like a reference. If you unset $GLOBALS['test'] then it copies as it should. This is a bug occurs in all versions of PHP4 on all platforms (from what I can tell). ------------------------------------------------------------------------ Edit this bug report at http://bugs.php.net/?id=12775&edit=1

« previous php.dev (#63346) next »