1. Object Clone clone
PHP4 Object-oriented features a big drawback is that objects are treated as a different data type, which makes it impossible to use many common OOP methods, such as design patterns. These methods rely on passing objects as references to other class methods rather than as values, while passing by value is the default practice for PHP. Fortunately, PHP5 solves this problem and now all objects are considered references by default. However, because all objects are treated as references instead of values, it is now more difficult to replicate objects. If you attempt to copy a referenced object, this will only point to the address location of the original object. To solve the replication problem, PHP provides an explicit way to clone clones ( keywords , not methods) objects.
You can clone an object by adding the Clone keyword in front of the object, as follows:
Destinationobject = Clone TargetObject;
To clone an object:
<?phpclass person{ var $name; var $sex; var $age; function construct ($name, $sex, $age) { $this->name = $name; $this->sex = $sex; $this->age = $age; } function say () { echo "my Name:". $this->name. ", Gender:". $this->sex. ", Age:". $this->age. "<br/>"; }} $person 1 = new Person ("33", "male", "at"), $person 2 = clone $person 1; Clone/Copy objects using the Clone keyword, creating a copy of an object $person3 = $person 1; This is not a copy of the object, but rather a copy of the object to access the object's Reference $person1->say (); Call the original object in the speech mode, print the original object in all the property values $person2->say (); Invokes the speaking method in the replica object, printing all the property values in the cloned object $person3->say (); Call the original object, and print all the property values in the original object?>
2. Magic Method Clone ()
A total of two objects were created in the above program, one of which is a copy of the Clone keyword. Two objects can be completely independent, but the values of the members and attributes are exactly the same. You can declare a magic method "clone ()" In a class if you need to re-assign the initial value of the member property to the cloned copy object at clone time. This method is called automatically when the object is cloned, so the cloned copy can be reinitialized by this method. The Clone () method does not require any parameters. Rewrite the code in the example above to add the Magic Method clone () to the class and reinitialize the member properties in the replica object.
<?phpclass person{ var $name; var $sex; var $age; function construct ($name, $sex, $age) { $this->name = $name; $this->sex = $sex; $this->age = $age; } function say () { echo "my Name:". $this->name. ", Gender:". $this->sex. ", Age:". $this->age. "<br/>"; } function Clone () { $this->name = "Li 44"; Re-assigns the Name property in the replica object $this->age = ten; Re-assigns the Age property in the replica object }} $person 1 = new Person ("33", "Male", "Max"), $person 2 = clone $person 1; Creates a copy of an object and automatically calls the Clone () method in the class $person1->say (); Call the original object in the speech mode, print the original object in all the property values $person2->say (); Invokes the speaking method in the replica object to print all the property values in the cloned object?>
Operation Result:
My name: Zhang 33, Gender: Male, Age: 23 My Name: Lee 44, Gender: Male, Age: 10
3. Strengthening of the Singleton class: banning cloning
For an object of a class, if you use the clone operator, you will copy a new object that is exactly the same as the current object, and the magic method of the class is automatically invoked at this time: Clone () (as long as there is this method in the class).
To implement a singleton class, you should "Prohibit cloning"of the object of this singleton class. In PHP, in order to prevent cloning of a singleton class object to break the above implementation form of a singleton class, it is often provided with an empty private (Private-Modified) clone () method.
First look at the effect of "do not ban cloning" :
<?phpclass Singetonbasic { private static $instance; Static variables to be privatized, prevent out-of-class modification private function construct () { //constructor privatization, outside class cannot create object directly} // Private Function Clone () { } //with private modifier before clone (), to prohibit cloning public static function getinstance () { ///Common public--method, external interface, static--does not use objects but accesses if (!) through the class name . Self:: $instance instanceof Self)) {//private static variable $instance is empty self :: $instance = new self (); Create a new object for itself and assign a value to the private variable $instance } return self:: $instance;//return private variable $instance}} $a = Singetonbasic:: GetInstance (); $b = Singetonbasic::getinstance (); Var_dump ($a = = = $b); The result is: Boolean true A and B point to the same object $c = Clone $a; Var_dump ($a = = = $c);//Result: Boolean false A and C point to not the same object?>
Run the result as
Boolean Trueboolean False
We "do no cloning" processing, that is, the above code
Private Function Clone () {} //private modifier before clone () to prohibit cloning
This line of code removes the comment.
Run the result as
Boolean truefatal Error:call to private Singetonbasic::clone ()
That is, Clone () is automatically called when cloning, but the method is modified by the private, can not be called directly outside the class, the result is an error.