In PHP, the $ this and $ that pointers use instances. In PHP, the $ this and $ that pointers define a special method named _ clone () using the instance PHP5, which is automatically called during object cloning, use the _ clone () method to create an instance with the $ this and $ that pointers in the original PHP.
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
PHP5 defines a special method named _ clone (), which is automatically called during object cloning and uses _ clone () the method will establish a pair with the original...