Basically, I need to store past versions of this object in its current state in an array. Something like:
public class MyClass{
$n = 0;
$array = array()
storeOldVersion(){
$this->array[] = clone $this;
}
}
I know I do something like “clone $this->n”, but how can I literally clone the MyClass object itself, and store it into an array the MyClass object holds?
Thanks
What you proposed actually works, except you had some incorrect code. The following version works:
A word of warning though
If you call storeOldVersion() more than once, as is, it will recursively clone the “array” that contains other clones and your object could get pretty massive exponentially. You should probably unset the array variable from the clone before storing it in the array..
e.g.