I have an array of objects. I know that objects get assigned by \"reference\" and arrays by \"value\". But when I assign the array, each element of the array is referencing
Just include this function in all of your classes. This will do a deep clone of all objects in case if you have arrays of objects within the object itself. It will trigger all of the __clone()
functions in these classes:
/**
* Clone the object and its properties
*/
public function __clone()
{
foreach ($this as $key => $property)
{
if(is_array($property))
{
foreach ($property as $i => $o)
{
if(is_object($o)) $this->$key[$i] = clone $o;
else $this->$key[$i] = $o;
}
}
else if(is_object($property)) $this->$key = clone $property;
else $this->$key = $property;
}
}