PHP Object-Oriented object cloning method

Source: Internet
Author: User
As we have already known, the invocation of the object by means of a reference, the real call is the same object, sometimes need to build a copy of the object, change the original object does not want to affect the copy, in PHP can be based on the current object to clone an identical object, The cloned copy and the original two objects are completely independent without interfering with each other.

Let's take a simple example to look at the use of clones:

<?phpheader ("Content-type:text/html;charset=utf-8"); class character{                                                       //define a role class public $name;p rotected $ Location;function __construct ($name, $location)              //Create constructor {$this->name = $name; $this->location = $location;} function Play () {                                                      //create method Echo ' I want to play '.  $this->name. $this->location;}} $character 1 = new Character (' Sub-cable ', ' Medium single ');                Instantiate a class $character2 = Clone $character 1;                               The instantiated class is then cloned into a $character1->play ();                                                    Call method execution echo ' <br/> '; $character 2->play ();

The result of the above example is ' I want to play a single ' in the cable.

What does it mean when we say that the cloned copy and the original independence do not interfere with each other?

Or the above example, just a little bit of a change.

<?phpheader ("Content-type:text/html;charset=utf-8"); class character{                                                       //define a role class public $name;p ublic $ Location;function __construct ($name, $location)              //Create constructor {$this->name = $name; $this->location = $location;} function Play () {                                                      //create method Echo ' I want to play '.  $this->name. $this->location;}} $character 1 = new Character (' Sub-cable ', ' Medium single ');                Instantiate a class $character2 = Clone $character 1; $character 2->location = ' on single '; $character 1->play ();                                                    Call method execution echo ' <br/> '; $character 2->play ();

The result of the above example is ' I want to play a single ' and ' I want to play a single ' on the Asian cable.

From the example, it can be seen that the cloned copy and the original two objects are completely independent without interfering with each other.

The use of __clone

Many times we don't just want to clone an object, we want the object to have its own properties and methods. Then we're going to create a __clone method in the class. This method is similar to a constructor and destructor because it is not called directly.

Take the example above:

<?phpheader ("Content-type:text/html;charset=utf-8"); class character{                                                       //define a role class public $name;p ublic $ Location;function __construct ($name, $location)              //Create constructor {$this->name = $name; $this->location = $location;} function __clone () {$this, location = ' on single ';} function Play () {                                                      //create method Echo ' I want to play '.  $this->name. $this->location;}} $character 1 = new Character (' Sub-cable ', ' Medium single ');                Instantiate a class $character2 = Clone $character 1; $character 1->play ();                                                    Call method execution echo ' <br/> '; $character 2->play ();

A good feature of the __clone method is that it can be called after a copy is created using the default behavior, so that at this stage, it is possible to change only what you want to change.

The most common feature that

adds in the __clone method is to ensure that class properties that are processed as references are correctly replicated. If you are cloning a class that contains an object reference, you might need to get a second copy of the object instead of the second reference to the object, so that is why you would add the code to the __clone method.

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.