$ This and $ that pointers in PHP use instances
PHP5 defines a special method named "_ clone ()", which is automatically called during object cloning and uses "_ clone () the method creates an object with the same attributes and methods as the original object. To change the content of the original object after cloning () override the original attributes and methods. The "_ clone ()" method can have no parameters. It automatically contains two pointers $ this and $ that, and $ this points to the duplicate, and $ that points to the original, the specific instance is as follows:
The Code is as follows:
Class Person {
// The following are the member attributes of a person.
Var $ name; // the name of a person.
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
// Function _ construct ($ name = "", $ sex = "", $ age = "")
Function _ construct ($ name, $ sex, $ age ){
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
}
// This person can talk about his/her attributes.
Function say (){
Echo "My name is:". $ this-> name. "Gender:". $ this-> sex. "My age is:". $ this
-> Age ."
";
}
// Method automatically called during object cloning. If you want to change the content of the original object after cloning, You need to rewrite the original attributes and methods in _ clone.
Function _ clone (){
// $ This refers to the copy p2, and $ that points to the original p1, so that the attributes of the copy are changed in this method.
$ This-> name = "I'm copying Michael Jacob $ that-> name ";
// $ This-> age = 30;
}
}
$ P1 = new Person ("Zhang San", "male", 20 );
$ P2 = clone $ p1;
$ P1-> say ();
$ P2-> say ();
?>
The result of successfully running this PHP program is as follows:
The Code is as follows:
My name is Zhang San Gender: male my age is: 20
My name is: I copied the three Gender: male, my age is: 20