|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-02-03 13:48 UTC] andi@php.net
[2004-02-04 18:02 UTC] auroraeosrose at hotmail dot com
[2004-08-07 19:45 UTC] curt@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 13:00:01 2025 UTC |
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 ) )