2

All resources online have been on "how to use", but nothing describes "when to use". Of course some is by preference, So I would like to see answers to when would be a good time to use cloning on objects.

$object_2 = (clone) $object_1;

Now $object_2 is a identical copy of $object_1 and when they make changes, they are changed locally to their own objects instead of by reference of each other.

So what is the best strategy with cloning?

tmarois
  • 2,424
  • 2
  • 31
  • 43
  • 1
    Hi. A good example described [here](http://stackoverflow.com/a/2144675/4469537) . – Sergey Chizhik Sep 11 '15 at 02:54
  • Thanks @SergeyChizhik It makes perfect sense on the cloning, and of course the idea is so that no one can alter an object because its a copy and not a reference to it. But also realizing you'd need to call all the internal objects too. – tmarois Sep 11 '15 at 10:59
  • 1
    You might do it manualy using magic `__clone`. This case described in [doc](http://php.net/manual/ru/language.oop5.cloning.php). It tells: When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables will remain references. – Sergey Chizhik Sep 11 '15 at 14:55

0 Answers0