对象实例的clone是地址复制,基础属性(非对象实例)是引用复制
<?php class C{ public $name = ‘eg‘; } class A{ public $color = ‘black‘; public $c; function __construct(){ $this->c = new C(); } } class B extends A{ public $sex =‘female‘; } //PHP的复制 $obj1 = new B(); $obj2 = $obj1; $obj2->color = ‘white‘; var_dump($obj1); echo ‘<br/>‘; var_dump($obj2); echo ‘<br/>‘; //PHP的clone $obj3 = clone $obj1; $obj3->color = ‘red‘; $obj3->c->name = ‘change‘; var_dump($obj3); echo ‘<br/>‘; echo($obj1->c->name).‘<br/>‘; echo($obj2->c->name).‘<br/>‘; echo($obj3->c->name).‘<br/>‘; ?>
时间: 2024-11-08 05:01:36