php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37231 Array of object's problem
Submitted: 2006-04-28 05:46 UTC Modified: 2006-04-28 07:39 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: espiao at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.1.2 OS: Debian
Private report: No CVE-ID: None
 [2006-04-28 05:46 UTC] espiao at gmail dot com
Description:
------------
I'm using an array of objects and when I copy the array to an another array structure this will referencing, not copying.
I tried to use copy() statement, but it works only to objects, off-course.
Soorry by bad english.

Reproduce code:
---------------
class slaveObject {
  public $singleVar;
  public __construct($mySingleVar) { $this->sigleVar = $mySingleVar; }
}
class mainObject {
  public $objects = array();
  public __construct() {
    $this->objects[] = new slaveObject('Example1');
    $this->objects[] = new slaveObject('Example2');
    $this->objects[] = new slaveObject('Example3');
  }
}

$myObject = &new mainObject();
$myObjectArray = $myObject->objects;
print_r($myObject->objects);
$myObjectArray[0]->singleVar = 'Example4';
print_r($myObject->objects);
print_r($myObjectArray);

Expected result:
----------------
Array(
  [0] => slaveObject (
    [singleVar] => Example1
  )
  [1] => slaveObject (
    [singleVar] => Example2
  )
  [2] => slaveObject (
    [singleVar] => Example3
  )
)
Array(
  [0] => slaveObject (
    [singleVar] => Example1
  )
  [1] => slaveObject (
    [singleVar] => Example2
  )
  [2] => slaveObject (
    [singleVar] => Example3
  )
)
Array(
  [0] => slaveObject (
    [singleVar] => Example4
  )
  [1] => slaveObject (
    [singleVar] => Example2
  )
  [2] => slaveObject (
    [singleVar] => Example3
  )
)

Actual result:
--------------
Array(
  [0] => slaveObject (
    [singleVar] => Example1
  )
  [1] => slaveObject (
    [singleVar] => Example2
  )
  [2] => slaveObject (
    [singleVar] => Example3
  )
)
Array(
  [0] => slaveObject (
    [singleVar] => Example4
  )
  [1] => slaveObject (
    [singleVar] => Example2
  )
  [2] => slaveObject (
    [singleVar] => Example3
  )
)
Array(
  [0] => slaveObject (
    [singleVar] => Example4
  )
  [1] => slaveObject (
    [singleVar] => Example2
  )
  [2] => slaveObject (
    [singleVar] => Example3
  )
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-28 05:54 UTC] espiao at gmail dot com
I'm using a monkey code to solve this:

$myObjectArray = array();
foreach ($myObject->objects as &$item)
  $myObjectArray[] = copy $item;

This code as an solution, but is really ugly. I belive that is as real bug.
 [2006-04-28 07:39 UTC] mike@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

OBject behaviour has changed from PHP4 to PHP5, please catch up on the manual.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC