设计模式(原型模式)
浅拷贝:拷贝一个对象作为另外一个对象的副本,之拷贝属性和方法。如果属性是对象拷贝的是对象的引用。
深度拷贝:拷贝一个对象作为另外对象的一个副本,包括该对象下的属性及方法,如果属性是对象也要拷贝而不是拷贝引用。
深度拷贝
<?php class Company { protected $name; protected $address; function __construct($name, $address){ $this->name = $name; $this->address = $address; } function getAddress(){ return $this->address; } function setName($name) { $this->name = $name; } function getName(){ return $this->name; } } class Person { protected $name; protected $age; public $company; function __construct($name,$age){ $this->name = $name; $this->age = $age; $this->company = new Company('北京','北京市海淀区'); } function changeCompanyName($name){ $this->company->setName($name); } function getName(){ return $this->name; } function getAge(){ return $this->age; } function getCompanyName(){ return $this->company->getName(); } function getCompanyAddress(){ return $this->company->getAddress(); } function __clone(){ $this->company = clone $this->company; } function getCompany(){ return $this->company; } } $p1 = new Person('jam.zhou', '88'); $p2 = clone $p1; $p1->changeCompanyName('深圳**公司'); echo $p1->getCompanyName(); echo "\n"; echo $p2->getCompanyName();
浅拷贝
<?php class Company { protected $name; protected $address; function __construct($name, $address){ $this->name = $name; $this->address = $address; } function getAddress(){ return $this->address; } function setName($name) { $this->name = $name; } function getName(){ return $this->name; } } class Person { protected $name; protected $age; public $company; function __construct($name,$age){ $this->name = $name; $this->age = $age; $this->company = new Company('北京','北京市海淀区'); } function changeCompanyName($name){ $this->company->setName($name); } function getName(){ return $this->name; } function getAge(){ return $this->age; } function getCompanyName(){ return $this->company->getName(); } function getCompanyAddress(){ return $this->company->getAddress(); } function getCompany(){ return $this->company; } } $p1 = new Person('jam.zhou', '88'); $p2 = clone $p1; $p1->changeCompanyName('深圳**公司'); echo $p1->getCompanyName(); echo "\n"; echo $p2->getCompanyName();
深圳拷贝如果引用的对象太多,可以不考虑使用__clone方法实现,而应该拷贝用序列号来实现。
<?php class Company { protected $name; protected $address; function __construct($name, $address){ $this->name = $name; $this->address = $address; } function getAddress(){ return $this->address; } function setName($name) { $this->name = $name; } function getName(){ return $this->name; } } class Person { protected $name; protected $age; public $company; function __construct($name,$age){ $this->name = $name; $this->age = $age; $this->company = new Company('北京','北京市海淀区'); } function changeCompanyName($name){ $this->company->setName($name); } function getName(){ return $this->name; } function getAge(){ return $this->age; } function getCompanyName(){ return $this->company->getName(); } function getCompanyAddress(){ return $this->company->getAddress(); } function getCompany(){ return $this->company; } } $p1 = new Person('jam.zhou', '88'); $p2 = unserialize(serialize($p1));