class SubObject
{
static $instances = 0;
public $instance;
public function __construct() {
$this->instance = ++self::$instances;
}
public function __clone() {
$this->instance = ++self::$instances;
}
}
class MyCloneable
{
public $object1;
public $object2;
function __clone()
{
// Force a copy of this->object, otherwise
// it will point to same object.
$this->object1 = clone $this->object1;
$this->object2 = clone $this->object2;
}
}
$obj = new MyCloneable();
$obj->object1 = new SubObject();
$obj->object2 = new SubObject();
$obj2 = clone $obj;
print("Original Object:<br>");
echo $obj->object1->instance."<br>";
echo $obj->object2->instance."<br>";
print("Cloned Object:<br>");
echo $obj2->object1->instance."<br>";
echo $obj2->object2->instance."<br>";
No comments:
Post a Comment