#27100 [NEW]: __clone is referencing object held inside a class instead of copying

From: Date: Fri, 30 Jan 2004 22:06:12 +0000
Subject: #27100 [NEW]: __clone is referencing object held inside a class instead of copying
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-54195@lists.php.net to get a copy of this message
From: auroraeosrose at hotmail dot com Operating system: Win XP Pro SP1 PHP version: 5CVS-2004-01-30 (dev) PHP Bug Type: Zend Engine 2 problem Bug description: __clone is referencing object held inside a class instead of copying Description: ------------ When a class holds an object of another class inside and the outside class is cloned, the inside class seems to be referenced instead of cloned along with the outside class If you'll see below, you'll notice that the Test class is cloned properly, the variable registered doesn't show up in $test but does in $test2 However, the array added to the list inside the Test2 class shows up in both, when it should only be in $test2 New php5 snap as of 1/30/2004 apache2 module - mysql only extra extension loaded Reproduce code: --------------- class Test { private $object; private $list = array(); public function __construct() { $this->object = new Test2(); } public function registerVar($var) { $this->list[] = $var; $key = end(array_keys($this->list)); $this->object->setList($key); return; } } class Test2 { public $list = array('globals' => array()); public function setList($id) { $id = (int) $id; $this->list[$id] = array(); return; } } $test = new test(); $test2 = $test->__clone(); $test2->registerVar('hello'); print_r($test); print_r($test2); Expected result: ---------------- Test Object ( [object:private] => Test2 Object ( [list] => Array ( [globals] => Array ( ) ) ) [list:private] => Array ( ) ) Test Object ( [object:private] => Test2 Object ( [list] => Array ( [globals] => Array ( ) [0] => Array ( ) ) ) [list:private] => Array ( [0] => hello ) ) Actual result: -------------- Test Object ( [object:private] => Test2 Object ( [list] => Array ( [globals] => Array ( ) [0] => Array ( ) ) ) [list:private] => Array ( ) ) Test Object ( [object:private] => Test2 Object ( [list] => Array ( [globals] => Array ( ) [0] => Array ( ) ) ) [list:private] => Array ( [0] => hello ) ) -- Edit bug report at http://bugs.php.net/?id=27100&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=27100&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=27100&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=27100&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=27100&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=27100&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=27100&r=needscript Try newer version: http://bugs.php.net/fix.php?id=27100&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=27100&r=support Expected behavior: http://bugs.php.net/fix.php?id=27100&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=27100&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=27100&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=27100&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=27100&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=27100&r=dst IIS Stability: http://bugs.php.net/fix.php?id=27100&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=27100&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=27100&r=float

« previous php.bugs (#54195) next »