|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-04-28 05:54 UTC] espiao at gmail dot com
[2006-04-28 07:39 UTC] mike@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 10:00:01 2025 UTC |
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 ) )