#27100 [Bgs->Opn]: shallow __clone copy behavior not documented in any place on php website

From: Date: Wed, 04 Feb 2004 23:02:46 +0000
Subject: #27100 [Bgs->Opn]: shallow __clone copy behavior not documented in any place on php website
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969358751@lists.php.net to get a copy of this message
ID: 27100 User updated by: auroraeosrose at hotmail dot com -Summary: __clone is referencing object held inside a class instead of copying Reported By: auroraeosrose at hotmail dot com -Status: Bogus +Status: Open -Bug Type: Zend Engine 2 problem +Bug Type: Documentation problem Operating System: * PHP Version: 5CVS-2004-01-30 (dev) New Comment: If this is the default behavior, it should be documented in some way. I'm aware that most of php5 is undocumented at this point, but the zend php5 changes page at http://www.php.net/zend-engine-2.php makes no mention that __clone is a shallow and not a deep copy - there should be a short paragraph that objects stired as properties are NOT cloned but instead referenced unless you override within a __clone method. It will save a lot of bug reports :) Random bug reports are not where people go looking for documentation on how __clone works, and according to the current docs, this is a bug, not a feature. Previous Comments: ------------------------------------------------------------------------ [2004-02-03 13:48:54] andi@php.net 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 Check the following bug report to see how we changed __clone()'s behavior. http://bugs.php.net/?id=26794 In any case, when we do a copy, we don't do a deep copy. You will have to handle that yourself by adding such code to your __clone() method. ------------------------------------------------------------------------ [2004-01-30 17:05:56] auroraeosrose at hotmail dot com 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 this bug report at http://bugs.php.net/?id=27100&edit=1

« previous php.doc (#969358751) next »