ID: 49964 Updated by: [email protected] Reported By: neoglez at gmail dot com -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: MWindows Prof. V.2002 SP3 PHP Version: 5.2SVN-2009-10-23 (snap) New Comment:
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 All elements of $this->array_of_object[] point to the same object. You have to use independent instances or clone the objects ... Previous Comments: ------------------------------------------------------------------------ [2009-10-23 07:18:21] neoglez at gmail dot com Description: ------------ When a member of a class(let's say class A)is an array of objects from other class B (who has a member that is an array too) and i want to populate the $this->member from the class A using $this->member[]=$some_object_of_class_b it does increase the number of elements in the array BUT IT OVERWRITES ALL ELEMENTS VALUE OF THE $this->member ARRAY. Reproduce code: --------------- class A{ public $arry = array(); public function set_array($array){ $this->arry = $array;} } class B{ public $array_of_object; public function add_object($object){ $this->array_of_object[] = $object; } } $array = array('I' => 'you'); $a = new A(); $b = new B(); for($i=0 ; $i < 3 ; $i++){ $array1 = array($i=>'he'); $array = array_merge($array, $array1); $a->set_array($array); $b->add_object($a);} var_dump($b); Expected result: ---------------- object(B)#2 (1) { ["array_of_object"]=> array(3) { [0]=> object(A)#1 (1) { ["arry"]=> array(2) { ["I"]=> string(3) "you" [0]=> string(2) "he" } } [1]=> object(A)#1 (1) { ["arry"]=> array(3) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" } } [2]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } } } Actual result: -------------- object(B)#2 (1) { ["array_of_object"]=> array(3) { [0]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } [1]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } [2]=> object(A)#1 (1) { ["arry"]=> array(4) { ["I"]=> string(3) "you" [0]=> string(2) "he" [1]=> string(2) "he" [2]=> string(2) "he" } } } } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49964&edit=1
