__clone () vs unserialize (serialize ())?

With reference to this question, I got another solution use

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

instead

$obj2 = clone $obj1;

Which one is better to use?

+3
source share
2 answers

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

: $obj1 __clone(), clone $obj1 $obj1, , $obj1. __clone() clone , , . , ... ... - .:-) , , , , , , , .

unserialize(serialize($obj1)), , , , .

+10

, , - __clone magic.

clone, , . http://php.net/manual/en/language.oop5.cloning.php , , .

+3

Source: https://habr.com/ru/post/1791410/


All Articles