Php object replication

Source: Internet
Author: User
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        ))

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.