This article mainly introduces how to use the _ clone () method in PHP programming. The __clone () method is equivalent to a shortest copy, which is the basic knowledge in PHP beginners, for more information, see
This article mainly introduces how to use the _ clone () method in PHP programming. The __clone () method is equivalent to a shortest copy, which is the basic knowledge in PHP beginners, for more information, see
You can define a _ clone () method in the object class to adjust the object cloning behavior. The code for this method will be executed during the clone operation. In addition to copying all existing object members to the target object, the operation specified by the _ clone () method is also executed. Modify the Corporate_Drone class and add the following methods:
Function _ clone () {$ this-> tiecolor = "blue ";}
Then, create a new Corporate_Drone object, add the value of the employee ID Member, clone the object, and then output some data to show that the tiecolor of the cloned object is indeed through _ 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 ()."
"; Echo" drone2 employeeID: ". $ drone2-> getEmployeeID ()."
"; Echo" drone2 tiecolor: ". $ drone2-> getTiecolor ()."
";?>
Program running result
Drone1 employeeID: 12345drone2 employeeID: 67890drone2 tiecolor:
Here is a small example:
<? Phpclass Fruit {private $ name = "Fruit"; private $ color = "color"; public function setName ($ name) {$ this-> name = $ name ;} public function setColor ($ color) {$ this-> color = $ color;} function showColor () {return $ this-> color. '. $ this-> name."
";} Function _ destruct () {echo" eaten (the object is recycled)
";}}$ Apple = new Fruit (); $ apple-> setName (" Big apple "); $ apple-> setColor (" red "); echo $ apple-> showColor (); $ clone_apple = $ apple; $ clone_apple-> setName ("Little apple"); $ clone_apple-> setColor ("cyan "); echo $ clone_apple-> showColor () ;?>
The above only assigns a class to another class, so the memory is still an object.
<? Phpclass Fruit {private $ name = "Fruit"; private $ color = "color"; public function setName ($ name) {$ this-> name = $ name ;} public function setColor ($ color) {$ this-> color = $ color;} function showColor () {return $ this-> color. '. $ this-> name."
";} Function _ destruct () {echo" eaten (the object is recycled)
";}Function _ clone () {$ this-> name =" clone Fruit ";}}$ apple = new Fruit (); $ apple-> setName ("Big apple"); $ apple-> setColor ("red"); echo $ apple-> showColor (); $ clone_apple = clone $ apple; $ clone_apple-> setColor ("cyan"); echo $ clone_apple-> showColor ();?>
The clone method cloned a new class, so there are two objects in the memory.
Php's _ clone () method is used to perform a shortest copy on an object instance. The basic value type in the object is copying the data transfer value, and the object type member variable in the object, if you do not overwrite the _ clone method and explicitly clone this object member variable, this member variable is passed to reference copying instead of generating a new object. as described in the 28th line comment in the following example:
<? 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 () {# COPY method, in which you can define the clone operation $ this-> Id = 0; $ this-> account = clone $ this-> account; # if this clause is not added, the account will be copied and referenced only in clone, the balance of one account is modified, and the other will also be modified.} $ 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 )}}