使用__clone()复制对象
class Account
{
//………
}
class Person
{
private
name;private
age;
private $account;
//构造方法中包含一个类
function __construct(
name,
age, Account account)
{this->name =
name;
this->age =
age;
this->account = $account;
}
function __clone()
{
//通过显示定义clone关键字,就会复制原始的对象
this−>account=clone
this->account;
}
我们看下上文中的代码
最显著的地方就是我们将clone函数重新写了一遍,这样在复制的时候就会重新生成一个新的对象
如果我们不进行重写,而写作
function __clone()
{
}
的形式,那么就不会复制引用对象
就会产生两个对象指向同一个对象的情况