In the given scenario, i have a class:
class shopCart {}
I use destructors and constructors in there, to perform certain actions.
Later on, i want to store a serialized version of the cart. However, the object i serialize should not incorporate the destructor and constructor methods.
In order to achieve this, i created a class:
class storageCart extends shopCart {
function __construct(){}
function __destruct(){}
}
Now, before i store the cart, i must create a new object of type storageCart
and make sure it contains all the properties of shopCart
.
To fulfill this need, shopCart
has a method getInstanceForStorage()
:
public function getInstanceForStorage() {
$storageCart = new storageCart();
foreach(get_object_vars($this) as $k => $v){
$storageCart->{$k}=$v;
}
return $storageCart;
}
The problem is that changes i make to the new instance (storageCart
) seem to affect the original instance (shopCart
).
It is important to mention here, that one of the variables of shopCart
is an array containing objects (cart items).
I assume i ran into some object / instancing / copying / cloning (or lack thereof) mess. But i don't know what the problem could be, because i do not use pointers in my assignment in the above foreach
loop.
Can anyone shed some light on what is happening here?
Note: This is PHP > 5.3
EDIT
I tried copying the contents of the array which contains the objects, by assigning them to the same array in the new object and using clone
, but - to my surprise - i still end up with a reference. I don't understand.
Is it perhaps possible, that objects within the cloned objects are still references to the original objects?