In most cases, we do not need to completely copy an object to obtain its attributes. However, you need to: if you have a GTK window object, the object holds window-related resources. You may want to copy a new window to keep all attributes consistent with the original one. In most cases, we do not need to completely copy an object to obtain the attributes. However, you need to: if you have a GTK window object, the object holds window-related resources. You may want to copy a new window to keep all attributes the same as the original window, but it must be a new object (because if it is not a new object, changes in one window will affect the other window ). Another case is: if object A contains the reference of object B, when you copy object A, you want to use the object instead of object B but A copy of object B, then you must get A copy of object. Object replication can be completed by using the clone keyword (if possible, the _ clone () method of the object will be called ). The _ clone () method in the object cannot be called directly.
$ Copy_of_object = clone $ object;
After the object is copied, PHP 5 performs a shallow copy on all the attributes of the object ). All the reference attributes will still be a reference pointing to the original variable.
Void _ clone (void)
When the replication is complete, if the _ clone () method is defined, the _ clone () method in the newly created object (the copied object) will be called, can be used to modify the attribute value (if necessary ).
Copy an object
Instance = + + self: $ instances;} public function _ clone () {$ this-> instance = + self: $ instances ;}} class MyCloneable {public $ object1; public $ object2; function _ clone () {// force copy this-> object, otherwise, it still points to the same object $ this-> object1 = clone $ this-> object1 ;}}$ obj = new MyCloneable (); $ obj-> object1 = new SubObject (); $ obj-> object2 = new SubObject (); $ obj2 = clone $ obj; print ("Original Object: \ n"); prin T_r ($ obj); print ("Cloned Object: \ n"); print_r ($ obj2);?>
The above routine will output:
Original Object:MyCloneable Object( [object1] => SubObject Object ( [instance] => 1 ) [object2] => SubObject Object ( [instance] => 2 ))Cloned Object:MyCloneable Object( [object1] => SubObject Object ( [instance] => 3 ) [object2] => SubObject Object ( [instance] => 2 ))