__clone() Method¶
To clone an object is the creation of a new distinct object, from an existing one. This is basically a copy, although the object itself is multiplied by call the clone operator.
By default PHP does a shallow clone, duplicating the scalars (string, integer…) and keeping the same reference to objects. To make a deep clone, the class must define the __clone
magic method.
Cloning is similar to creating another object of the same class, without requiring all the constructor arguments.
<?php
$spike = new Dog('Spike', 'Teckel', 'red');
$medor = clone $spike;
$medor->setCollar('green'); // distinguish spike and medor by collar
?>
See also How to clone an object in PHP
Related : Readonly, Magic Methods, Deep Clone, Shallow Clone