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 );
?