Home > Article > Backend Development > How to call __clone() in PHP
This article mainly introduces how to call __clone() in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
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. Next, modify the Corporate_Drone class 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 passed_ Set by 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 running result
drone1 employeeID: 12345 drone2 employeeID: 67890 drone2 tiecolor:
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 __clone is not overridden Method, if you explicitly clone the object member variable, the member variable is copied by reference instead of generating a new object. As the comment on line 28 of the following example 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:
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) } }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php paging principle paging code paging class production method example detailed explanation
php method of deleting file directories based on recursion
The above is the detailed content of How to call __clone() in PHP. For more information, please follow other related articles on the PHP Chinese website!