php - __clone method is not making copy of class object -
i want use __clone() making copy of class object , after want print copy object. here code:
<?php class test { public static $counter = 0; public $id; public $other; public function __construct(){ $this->id = self::$counter++; } public function __clone() { $this->other = $that->other; $this->id = self::$counter++; } } $obj = new test(); $copy = clone $obj; print_r($copy); ?>
instead of
$copy = $obj->__clone();
you should use
$copy = clone $obj;
the __clone()
method called when call clone
Comments
Post a Comment