Detailed explanation of the use of __clone() method in PHP programming_php skills

WBOY
Release: 2016-05-16 20:04:05
Original
1030 people have browsed it

You can define a __clone() method in the object class to adjust the cloning behavior of the object. The code for this method will be executed during the cloning operation. In addition to copying all existing object members to the target object, the operations specified by the __clone() method are performed. Modify the Corporate_Drone class below and add the following methods:

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

After that, create a new Corporate_Drone object, increase the value of the employeeid member, clone the object, and then output some data to show that the tiecolor of the cloned object is indeed set through the __clone() method. Sample code:

<&#63;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 />";
&#63;>
Copy after login

Program execution results

drone1 employeeID: 12345
drone2 employeeID: 67890
drone2 tiecolor:
Copy after login

Another small example:

<&#63;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();
&#63;>

Copy after login

The above just assigns one class to another class, so there is still an object in the memory at this time.

<&#63;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();
&#63;>

Copy after login

The clone method clones a new class, so there are two objects in the memory at this time.

PHP’s __clone() method makes a shallow copy of an object instance. The basic numerical types in the object are copied by value, and the object-type member variables in the object, if the __clone method is not overridden, will obviously If the object member variable is cloned in the form, the member variable is copied by reference instead of generating a new object. As the comment on line 28 of the example below says

<&#63;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);
  
 &#63;>

Copy after login

Output:

Copy code The code is as follows:

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) } }
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template