I faced a situation: I had an objects scene, containing a lot of objects referencing each other, and wanted to be able to take a snapshot of this scene and tweak it with the ability of “resetting” everything to a previous state.
Cloning was the first think I thought of. If we have a look at the following example::
We can note that the two Bs are equals. This makes sense since PHP can’t guess what you wan’t to do, and just copy by default the fields from the original class to the cloned one.
Now, suppose that you want an entire clone (no cross-references)
One solution would be writing a
__clone() method to explicitely clone the instance, the problem is that this can quickly become huge if there is many objects and references.
That’s why I eventually noticed that the PHP serialize() method was clever enough to figure out when references are in the objects it’s cloning (even self-references), and that’s why this strange-but-powerful idea:
Doing this, you will clone
$y, but also clone all the references it contains. You can be sure that
$y won’t reference a previously existing object.