欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP编程中的__clone()方法使用详解

程序员文章站 2023-08-22 13:41:44
可以在对象类中定义一个__clone()方法来调整对象的克隆行为。此方法的代码将在克隆操作期间执行。除了将所有现有对象成员复制到目标对象之外,还会执行__clone()方法...

可以在对象类中定义一个__clone()方法来调整对象的克隆行为。此方法的代码将在克隆操作期间执行。除了将所有现有对象成员复制到目标对象之外,还会执行__clone()方法指定的操作。下面修改corporate_drone类,增加以下方法:

function __clone() { 
 $this->tiecolor = "blue";
}

之后,创建一个新的corporate_drone对象,增加employeeid成员的值,克隆这个对象,然后输出一些数据,从而显示克隆对象的tiecolor确实是通过__clone()方法设置的。示例代码:

<?php
 // create new corporatedrone object
  $drone1 = new corporatedrone();
 
 // set the $drone1 employeeid member
  $drone1->setemployeeid("12345");
 
 // clone the $drone1 object
  $drone2 = clone $drone1;
 
 // set the $drone2 employeeid member
  $drone2->setemployeeid("67890");
 
 // output the $drone1 and $drone2 employeeid members
  echo "drone1 employeeid: ".$drone1->getemployeeid()."<br />";
  echo "drone2 employeeid: ".$drone2->getemployeeid()."<br />";
  echo "drone2 tiecolor: ".$drone2->gettiecolor()."<br />";
?>

程序运行结果

drone1 employeeid: 12345
drone2 employeeid: 67890
drone2 tiecolor:

再来一个小例子:

<?php
class fruit {
 private $name = "水果";
 private $color = "颜色";
 
 public function setname($name){
 $this->name = $name;
 }
 
 public function setcolor($color){
 $this->color = $color;
 }
 
 function showcolor(){
 return $this->color.'的'.$this->name."<br />";
 }
 
 function __destruct(){
 echo "被吃掉了(对象被回收) <br />"; 
 }
}

$apple = new fruit();
$apple->setname("大苹果");
$apple->setcolor("红色");
echo $apple->showcolor();

$clone_apple = $apple;
$clone_apple->setname("小苹果");
$clone_apple->setcolor("青色");

echo $clone_apple->showcolor();
?>

上面只是将一个类赋值给另一个类,所以此时内存中仍是一个对象。

<?php
class fruit {
 private $name = "水果";
 private $color = "颜色";
 
 public function setname($name){
 $this->name = $name;
 }
 
 public function setcolor($color){
 $this->color = $color;
 }
 
 function showcolor(){
 return $this->color.'的'.$this->name."<br />";
 }
 
 function __destruct(){
 echo "被吃掉了(对象被回收) <br />"; 
 }
 function __clone(){
 $this->name = "克隆水果"; 
 }
}

$apple = new fruit();
$apple->setname("大苹果");
$apple->setcolor("红色");
echo $apple->showcolor();

$clone_apple = clone $apple;
$clone_apple->setcolor("青色");

echo $clone_apple->showcolor();
?>

clone方法克隆出了一个新的类,所以此时内存中有两个对象。

php的__clone()方法对一个对象实例进行的浅复制,对象内的基本数值类型进行的是传值复制,而对象内的对象型成员变量,如果不重写__clone方法,显式的clone这个对象成员变量的话,这个成员变量就是传引用复制,而不是生成一个新的对象.如下面一个例子的第28行注释所说

<?php
  class account {
    public $balance;
    
    public function __construct($balance) {
      $this->balance = $balance;
    }
  }
 
  class person {
    private $id;
    private $name;
    private $age;
    public $account;
    
    public function __construct($name, $age, account $account) {
      $this->name = $name;
      $this->age = $age;
      $this->account = $account;
    }
    
    public function setid($id) {
      $this->id = $id;
    }
    
    public function __clone() {  #复制方法,可在里面定义再clone是进行的操作
      $this->id = 0;
      $this->account = clone $this->account;  #不加这一句,account在clone是会只被复制引用,其中一个account的balance被修改另一个也同样会被修改
    }
  }
  
  $person = new person("peter", 15, new account(1000));
  $person->setid(1);
  $person2 = clone $person;
  
  $person2->account->balance = 250;
  
  var_dump($person, $person2);
  
 ?>

输出:

复制代码 代码如下:

object(person)#1 (4) { ["id":"person":private]=> int(1) ["name":"person":private]=> string(5) "peter" ["age":"person":private]=> int(15) ["account"]=> object(account)#2 (1) { ["balance"]=> int(1000) } } object(person)#3 (4) { ["id":"person":private]=> int(0) ["name":"person":private]=> string(5) "peter" ["age":"person":private]=> int(15) ["account"]=> object(account)#4 (1) { ["balance"]=> int(250) } }