Php object-oriented object cloning method

Source: Internet
Author: User
We already know that the object is called using the address reference method. the object actually calls the same object, and sometimes a copy of the object needs to be created, when changing the original object, you do not want to affect the copy. in PHP, you can clone an identical object based on the current object, the cloned copy is completely independent of the original two objects and does not interfere with each other. Let's take a simple example to look at the clone usage: as we know before, the object is called using the address-based reference method and the object is actually called, sometimes you need to create a copy of the object. if you change the original object, you do not want to affect the copy. in PHP, you can clone an identical object based on the current object, the cloned copy is completely independent of the original two objects and does not interfere with each other.

Let's take a simple example to look at the clone usage:

 Name = $ name; $ this-> location = $ location;} function play () {// create method echo 'I want to play '. $ this-> name. $ this-> location ;}$ character1 = new Character ('assor', 'Singles'); // instantiate a class $ character2 = clone $ character1; // clone the instantiated class to a $ character1-> play (); // call the method to execute echo'
'; $ Character2-> play ();

The results of the above instances are all 'I want to play as 中 '.

As we have mentioned above, the cloned copy is independent of the original one and does not interfere with each other. what does this sentence mean?

Or the above instance, just a slight change.

 Name = $ name; $ this-> location = $ location;} function play () {// create method echo 'I want to play '. $ this-> name. $ this-> location ;}$ character1 = new Character ('assor', 'Singles'); // instantiate a class $ character2 = clone $ character1; $ character2-> location = 'Order list'; $ character1-> play (); // call the method to execute echo'
'; $ Character2-> play ();

The result of the above example is 'I want to play Asso MON' and' I want to play Asso on singles '.

The example shows that the cloned copy is completely independent from the original two objects without interfering with each other.

_ Clone usage

In many cases, we do not only need to clone an object, but also want the object to have its own attributes and methods. Then we need to create a _ clone method in the class. This method is similar to constructor and Destructor because it is not called directly.

Take the preceding example as an example:

 Name = $ name; $ this-> location = $ location;} function _ clone () {$ this-> location = 'Order up';} function play () {// create method echo 'play '. $ this-> name. $ this-> location ;}$ character1 = new Character ('assor', 'Singles'); // instantiate a class $ character2 = clone $ character1; $ character1-> play (); // call the method to execute echo'
'; $ Character2-> play ();

A good feature of The _ clone method is that it can be called after a copy is created using the default behavior. in this way, only the content to be changed can be changed at this stage.

The most common function added in the _ clone method is to ensure that the class attributes processed as references can be correctly copied. If you want to clone a class that contains object references, you may need to obtain the second copy of the object instead of the second reference of the object, therefore, this is why the code should be added to the _ clone method.

The above is a detailed description of php object-oriented object cloning method. For more information, see other related articles in the first PHP community!

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.