Search code examples
phpclassvariablesobjectclone

Stop new object from updating with old object's variables


I'm trying to do something like:

$obj2 = $obj1

where $var1 is an object, the problem is that I want $obj2 to be like a snap shot of $obj1 - exactly how it is at that moment, but as $obj1's variables change, $obj2's change as well. Is this even possible? Or am I going to have to create a new "dummy" class just so I can create a clone?


Solution

  • Simply clone the object, like so:

    $obj2 = clone $obj1;
    

    Any modifications to the members of $obj1 after the above statement will not be reflected in $obj2.