The reason for using the clone is that when working with an object, PHP always returns the object as a link, and not as a copy.
That's why when passing an object to a function, you don’t need to specify it and (link):
function doSomethingWithObject(MyObject $object) {
...
}
, , clone
, php :
class Obj {
public $obj;
public function __construct() {
$this->obj = new stdClass();
$this->obj->prop = 1;
}
function getObj(){
return $this->obj;
}
}
$obj = new Obj();
$a = $obj->obj;
$b = $obj->getObj();
$b->prop = 7;
var_dump($a === $b);
var_dump($a->prop, $b->prop, $obj->obj->prop);
$c = clone $a;
$c->prop = -3;
var_dump($a === $c);
var_dump($a->prop, $c->prop, $obj->obj->prop);