Motivo de uso de clon es que PHP cuando trabaja con objeto siempre devuelve el objeto como referencia, no como una copia.
Esa es la razón por objeto al pasar a una función que no es necesario especificar con & (referencia):
function doSomethingWithObject(MyObject $object) { // it is same as MyObject &object
...
}
Así que con el fin de obtener el objeto copiar usted tiene que utilizar la palabra clave clon Este es un ejemplo de cómo los objetos se manejan por php y lo hace clon:
class Obj {
public $obj;
public function __construct() {
$this->obj = new stdClass();
$this->obj->prop = 1; // set a public property
}
function getObj(){
return $this->obj; // it returns a reference
}
}
$obj = new Obj();
$a = $obj->obj; // get as public property (it is reference)
$b = $obj->getObj(); // get as return of method (it is also a reference)
$b->prop = 7;
var_dump($a === $b); // (boolean) true
var_dump($a->prop, $b->prop, $obj->obj->prop); // int(7), int(7), int(7)
// changing $b->prop didn't actually change other two object, since both $a and $b are just references to $obj->obj
$c = clone $a;
$c->prop = -3;
var_dump($a === $c); // (boolean) false
var_dump($a->prop, $c->prop, $obj->obj->prop); // int(7), int(-3), int(7)
// since $c is completely new copy of object $obj->obj and not a reference to it, changing prop value in $c does not affect $a, $b nor $obj->obj!
El [patrón Prototipo Diseño] (http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref = sr_1_1) también es un buen caso de uso. – pce