Deep clone
Posted: Wed Dec 13, 2006 7:03 pm
Does anybody have any techniques for deep clones where the objects inside the object being cloned are also cloned. I tried this but it doesn't work for privates or protected (same with foreach ($this))Output:Its important the solution works with inheritance as ideally I'd like to implement this method in an abstract superclass and have it affect all subclasses (privates have been used).
I'm trying to avoid endless:in my classes all over the place.
Code: Select all
private $a, $b, $c;
public function __construct()
{
$this->a = new stdClass();
$this->b = new stdClass();
$this->c = new stdClass();
}
public function __clone()
{
$reflection = new ReflectionClass($this);
foreach ($reflection->getProperties() as $prop) {
$prop = $prop->getName();
if (is_object($this->{$prop})) {
echo $prop;
$this->{$prop} = clone $this->{$prop};
}
}
}Code: Select all
I'm trying to avoid endless:
Code: Select all
public function __clone()
{
parent::__clone();
$this->_obj = clone $this->_obj;
$this->_obj1 = clone $this->_obj1;
$this->_obj2 = clone $this->_obj2;
}