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.