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"; }
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:
<?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 />"; ?>
Program execution results
drone1 employeeID: 12345 drone2 employeeID: 67890 drone2 tiecolor:
Another small example:
<?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(); ?>
The above just assigns one class to another class, so there is still an object in the memory at this time.
<?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(); ?>
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
<?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); ?>
Output: