Php object clone application

Source: Internet
Author: User
Php object clone uses 1 .??? PHP5 defines a _ clone () object that will have the same attributes and methods as the original object. If you want to change the content of the original object after cloning, you need to re-write the original attributes and methods in _ clone .?????? _ Clone () can have no parameters. it automatically contains the $ this and $ that pointers for php object clone.

1 .?

? ? PHP5 defines __Clone() An object with the same attributes and methods as the original object will be created. To change the content of the original object after cloning __Clone() Rewrite the original attributes and methods.

?

????? __Clone() It can have no parameters. it automatically contains two pointers: $ this and $ that.

$ This points to the duplicate

$ That points to the original

?

2.

If a new class is declared in the class

Class ConcretePrototype {
?? ? Public function _ construct (){
?? ??? ? $ This-> id = 1;
?? ??? ? $ This-> obj = new StdClass ();
?? ??? ? $ This-> obj-> name = 'dashu ';
?? ?}
?? ?
?? ? Public function myclone (){
?? ??? ? Return clone $ this;
?? ?}
???

???? // Without this, $ obj is a reference. If yes, it is two independent of each other. try it for yourself.
???? Private function _ clone (){
?? ??? ? $ This-> obj = clone $ this-> obj;
?? ?}
}

$ P = new ConcretePrototype ();
$ Q = $ p-> myclone ();
Var_dump ($ p, $ q );
$ Q-> id = 2;
// Change the obj attribute of $ q. The corresponding attribute of $ p does not change.
$ Q-> obj-> name = 'xiayi ';
Var_dump ($ p, $ q );

?

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.