时间:2021-07-01 10:21:17 帮助过:11人阅读
classSubObject
{static$instances = 0;
public$instance;
publicfunction__construct()
{$this->instance = ++ self::$instances;
}
publicfunction__clone()
{$this->instance = ++self::$instances;
}
}
classMyCloneable
{public$object1;
public$object2;
publicfunction__clone()
{// 强制复制一份this->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:\n");
print_r($obj);
print("Clone Object:\n");
print_r($obj2);
结果
Original Object:
MyCloneable Object
(
[object1] => SubObject Object
(
[instance] => 1
)
[object2] => SubObject Object
(
[instance] => 2
)
)
Clone Object:
MyCloneable Object
(
[object1] => SubObject Object
(
[instance] => 3//深复制 所以有+1
)
[object2] => SubObject Object
(
[instance] => 2//浅复制 并没有创建新的对象
)
)
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了PHP Clone 深复制和浅复制,包括了clone,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。