This article mainly introduces the $ this and $ that pointer usage instances in PHP. It can also be said that they have different usage details. the instance can be understood at a glance, you can refer to PHP5 to define a special method name "_ clone ()", which is automatically called during object cloning, use the "_ clone ()" method to create an object with 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 override the original attributes and methods in _ clone (). The "_ clone ()" method can have no parameters. it automatically contains the $ this and $ that pointers, $ this points to the duplicate, while $ that points to the original. the specific instance is as follows:
The code is as follows:
<? Php
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