php
2class SubObject 3{ 4static$instances= 0
; 5public$instance; 6 7publicfunction __construct() 8 { 9$this->instance = ++self::
$instances; 10 } 1112publicfunction __clone() 13 { 14$this->instance = ++self::
$instances; 15 } 16} 1718class MyCloneable 19{ 20public$object1; 21public$object2; 2223function __clone() 24 { 25// 强制复制一份this->object, 否则仍然指向同一个对象26$this->object1 =
clone$this->
object1; 27 } 2829function cloneTest() 30 { 31echo'cloneTest'
; 32 } 33} 3435$obj=
new MyCloneable(); 3637$obj->object1 =
new SubObject(); 38$obj->object2 =
new SubObject(); 3940$obj2=
clone$obj; 4142print("Original Object:\n"
); 43print_r(
$obj); 4445print("Cloned Object:\n"
); 46print_r(
$obj2); 47echo$obj2->cloneTest().":\n"
; 48echo(
newReflectionclass(
$obj2));
来源: http://www.cnblogs.com/XiongMaoMengNan/p/6674406.html