原文地址: http://www.nowamagic.net/librarys/posts/php/32
PHP4面向对象功能一个很大的缺点,是将对象视为另一种数据类型,这使得很多常见的OOP方法无法使用,如设计模式。这些方法依赖于将对象作为引用传递给其他类方法,而不是作为值传递,而按值传递却是PHP的默认做法。幸好,PHP5解决了这个问题,现在所有对象在默认情况下都被视为引用。但是,由于所有对象都被视为引用而不是值,所以现在复制对象更为困难。如果尝试复制一个引用的对象,这只会指向原对象的地址位置。为了解决复制问题,PHP提供了一种克隆(clone)对象的显式方法。
对象克隆 可以在对象前面加clone关键字来克隆对象,如下:
1 destinationObject = clone targetObject;
下面将给出一个详细的对象克隆示例。
这个例子使用一个名为Corporate_Drone的示例类,它包含两个成员(employeeid和tiecolor),并有相应的获取方法和设置方法。
此代码首先实例化一个corporatedrone对象,并以此为基础展示克隆操作的效果:
1 <?php 2 class corporatedrone { 3 private $employeeid; 4 private $tiecolor; 5 // Define a setter and getter for $employeeid 6 function setEmployeeID($employeeid) { 7 $this->employeeid = $employeeid; 8 } 9 function getEmployeeID() { 10 return $this->employeeid; 11 } 12 // Define a setter and getter for $tiecolor 13 function setTiecolor($tiecolor) { 14 $this->tiecolor = $tiecolor; 15 } 16 function getTiecolor() { 17 return $this->tiecolor; 18 } 19 } 20 // Create new corporatedrone object 21 $drone1 = new corporatedrone(); 22 23 // Set the $drone1 employeeid member 24 $drone1->setEmployeeID("12345"); 25 26 // Set the $drone1 tiecolor member 27 $drone1->setTiecolor("red"); 28 29 // Clone the $drone1 object 30 $drone2 = clone $drone1; 31 32 // Set the $drone2 employeeid member 33 $drone2->setEmployeeID("67890"); 34 35 // Output the $drone1 and $drone2 employeeid members 36 echo "drone1 employeeID: ".$drone1->getEmployeeID()."<br />"; 37 echo "drone1 tie color: ".$drone1->getTiecolor()."<br />"; 38 echo "drone2 employeeID: ".$drone2->getEmployeeID()."<br />"; 39 echo "drone2 tie color: ".$drone2->getTiecolor()."<br />"; 40 ?>
输出结果为:
drone1 employeeID: 12345
drone1 tie color: red
drone2 employeeID: 67890
drone2 tie color: red
$drone2变成一个Corporate_Drone类型的对象,并继承了$drone1的成员值。为进一步展示$drone2确实是Comporate_Drone类型,这里还重新对employeeid成员进行了赋值
__clone()方法
可以在对象类中定义一个__clone()方法来调整对象的克隆行为。此方法的代码将在克隆操作期间执行。除了将所有现有对象成员复制到目标对象之外,还会执行__clone()方法指定的操作。下面修改Corporate_Drone类,增加以下方法:
1 function __clone() { 2 $this->tiecolor = "blue"; 3 }
之后,创建一个新的Corporate_Drone对象,增加employeeid成员的值,克隆这个对象,然后输出一些数据,从而显示克隆对象的tiecolor确实是通过__clone()方法设置的。
示例代码:
1 <?php 2 // Create new corporatedrone object 3 $drone1 = new corporatedrone(); 4 5 // Set the $drone1 employeeid member 6 $drone1->setEmployeeID("12345"); 7 8 // Clone the $drone1 object 9 $drone2 = clone $drone1; 10 11 // Set the $drone2 employeeid member 12 $drone2->setEmployeeID("67890"); 13 14 // Output the $drone1 and $drone2 employeeid members 15 echo "drone1 employeeID: ".$drone1->getEmployeeID()."<br />"; 16 echo "drone2 employeeID: ".$drone2->getEmployeeID()."<br />"; 17 echo "drone2 tiecolor: ".$drone2->getTiecolor()."<br />"; 18 ?>
输出结果为:
drone1 employeeID: 12345
drone2 employeeID: 67890
drone2 tiecolor:
将一个类赋值给另一个类,此时内存中仍是一个对象:
1 <?php 2 class Fruit { 3 private $name = "水果"; 4 private $color = "颜色"; 5 6 public function setName($name){ 7 $this->name = $name; 8 } 9 10 public function setColor($color){ 11 $this->color = $color; 12 } 13 14 function showColor(){ 15 return $this->color.'的'.$this->name."<br />"; 16 } 17 18 function __destruct(){ 19 echo "被吃掉了(对象被回收) <br />"; 20 } 21 } 22 23 $apple = new Fruit(); 24 $apple->setName("大苹果"); 25 $apple->setColor("红色"); 26 echo $apple->showColor(); 27 28 $clone_apple = $apple; 29 $clone_apple->setName("小苹果"); 30 $clone_apple->setColor("青色"); 31 32 echo $clone_apple->showColor(); 33 ?>
输出结果为:
红色的大苹果
青色的小苹果
被吃掉了(对象被回收)
clone方法克隆出了一个新的类,此时内存中有两个对象:
1 <?php 2 class Fruit { 3 private $name = "水果"; 4 private $color = "颜色"; 5 6 public function setName($name){ 7 $this->name = $name; 8 } 9 10 public function setColor($color){ 11 $this->color = $color; 12 } 13 14 function showColor(){ 15 return $this->color.'的'.$this->name."<br />"; 16 } 17 18 function __destruct(){ 19 echo "被吃掉了(对象被回收) <br />"; 20 } 21 function __clone(){ 22 $this->name = "克隆水果"; 23 } 24 } 25 26 $apple = new Fruit(); 27 $apple->setName("大苹果"); 28 $apple->setColor("红色"); 29 echo $apple->showColor(); 30 echo "<hr/>"; 31 $clone_apple = clone $apple; 32 $clone_apple->setColor("青色"); 33 34 echo $clone_apple->showColor(); 35 ?>
输出结果为:
红色的大苹果
青色的克隆水果
被吃掉了(对象被回收)
被吃掉了(对象被回收)