Php__clone () Cloning methods and examples of shallow cloning and deep cloning use

Source: Internet
Author: User
can be inObject defines a clone () method in a class to adjust the cloning of an objectBehavior . 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 actions specified by the Clone () method are also executed. Modify the Corporate_drone class below to add the following methods:

function Clone () {  $this->tiecolor = "Blue";}

After that, a new Corporate_drone object is created, the value of the EmployeeID member is added, the object is cloned, and some data is output to show that the tiecolor of the cloned object is indeed set by the Clone () method. Example code:

<?php//Create New Corporatedrone object  $drone 1 = new Corporatedrone ();  Set the $drone 1 EmployeeID member  $drone 1->setemployeeid ("12345");  Clone the $drone 1 object  $drone 2 = clone $drone 1;  Set the $drone 2 EmployeeID member  $drone 2->setemployeeid ("67890");  Output the $drone 1 and $drone 2 EmployeeID members  echo "DRONE1 EmployeeID:". $drone 1->getemployeeid (). <br/> ";  echo "Drone2 EmployeeID:". $drone 2->getemployeeid (). " <br/> ";  echo "Drone2 tiecolor:". $drone 2->gettiecolor (). " <br/> ";? >

Program Run Results

Drone1 employeeid:12345drone2 employeeid:67890drone2 Tiecolor:

One more 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. ' <br/> "; }  function Destruct () {echo ' was eaten (object is recycled) <br/> ";  }} $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 simply assigns a class to another class, so this is still an object in memory.

<?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. ' <br/> "; }  function Destruct () {echo ' was eaten (object is recycled) <br/> ";  } 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 clones a new class, so there are two objects in memory at this time.

The PHP Clone () method makes a shallow copy of an object instance, and the underlying numeric type in the object is a copy of the value, and the object type member variable within the object, if the Clone method is not overridden, the member variable is a copy of the object that is referred to as the clone, Instead of generating a new object. As the 28th line of comments in 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 () {  #复制方法, which can be defined inside the clone is the operation      $this->id = 0;      $this->account = Clone $this->account;  #不加这一句, the account in clone will only be copied by reference, where one of the account's balance is modified and another is also modified    }  }    $person = new Person ("Peter", 15, New account (+));  $person->setid (1);  $person 2 = clone $person;    $person 2->account->balance = +;    Var_dump ($person, $person 2);   ? >

Shallow clone: Only non-object non-resource data in the cloned object, that is, the object's property is stored in the object type, the clone will appear incomplete

<?phpclass b{Public $val = 10;} Class a{Public $val =, public $b, public function construct () {  $this->b = new B ();}} $obj _a = new A () $obj _b = Clone $obj _a; $obj _a->val = $obj _a->b->val = 40;var_dump ($obj _a); Echo ' <br> '; v Ar_dump ($obj _b);

The results of the operation are as follows:

Object (a) [1] public ' val ' = = int-Public ' b ' = = object (b) [2] public  ' val ' = + int + object (A) [3] public ' VA L ' = = int public ' b ' = = object (b) [2] public  ' val ' + int 40

Deep cloning: All property data for an object is copied completely, and the Magic Method clone () is used to implement deep cloning inside

<?phpclass b{Public $val = 10;} Class a{Public $val =, public $b, public function construct () {  $this->b = new B (),} public Function Clone () {
   $this->b = Clone $this->b; }} $obj _a = new A (), $obj _b = Clone $obj _a; $obj _a->val = $obj _a->b->val = 40;var_dump ($obj _a); Echo ' <br> ' ; Var_dump ($obj _b);

The results of the operation are as follows:

Object (a) [1] public ' val ' = = int-Public ' b ' = = object (b) [2] public  ' val ' = + int + object (A) [3] public ' VA L ' = = int public ' b ' = = object (b) [4] public  ' val ' + int 10

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.