Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__clone() vs unserialize(serialize())?

With the reference of this question, I got an another solution is use

$obj2 = unserialize(serialize($obj1));

instead of

$obj2 = clone $obj1;

Which one is better to use?

like image 277
Gaurav Avatar asked Dec 12 '22 15:12

Gaurav


1 Answers

tl;dr version: Use clone for simple objects and trees, unserialize(serialize()) for complicated graphs of objects.

Longer explanation: Unless $obj1 implements __clone(), the expression clone $obj1 will return a shallow copy of $obj1, but sharing the contents of objects pointed to by $obj1. Even if __clone() is implemented to perform a deep copy by recursive clone of members, it will only work safely if the object graph is a tree. If the object graph contains cycles, it will recurse indefinitely and... well... this is Stack Overflow for a reason. :-) If it is a directed acyclic graph but not a tree, any object referenced multiple times will have those multiple references replaced by copies, which may not be what you want.

unserialize(serialize($obj1)), on the other hand, will deal with cycles in the object graph, but is more expensive in terms of both CPU time and memory.

like image 170
Jeffrey Hantin Avatar answered Dec 15 '22 05:12

Jeffrey Hantin