php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49964 Array member of a class is not well populated
Submitted: 2009-10-23 07:18 UTC Modified: 2009-10-23 10:31 UTC
From: neoglez at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2SVN-2009-10-23 (snap) OS: MWindows Prof. V.2002 SP3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: neoglez at gmail dot com
New email:
PHP Version: OS:

 

 [2009-10-23 07:18 UTC] 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"
      }
    }
  }
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-23 10:31 UTC] johannes@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

All elements of $this->array_of_object[]  point to the same object. You have to use independent instances or clone the objects ...
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 07:01:26 2025 UTC