Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > #37231 : Array of object's problem
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 31st May 05:39
php-bugs
External User
 
Posts: 1
Default #37231 : Array of object's problem



ID: 37231
User updated by: espiao at gmail dot com
Reported By: espiao at gmail dot com
Status: Open
Bug Type: Arrays related
Operating System: Debian
PHP Version: 5.1.2
New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[2006-04-28 05:46:32] 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
)
)


------------------------------------------------------------------------


--
Edit this bug report at http://bugs.php.net/?id=37231&edit=1
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666