Usage of the _ clone () method in PHP programming

Source: Internet
Author: User
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, you can refer to defining 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, which can be defined in Clone is the operation $ this-> id = 0; $ this-> account = clone $ this-> account; # Do not add this sentence, in clone, the account will be copied and referenced only, and the balance of one account 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 )}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.