How to explain the two $ this in the _ clone () method of php? What are their meanings? {Code...}: how does one explain the two $ this in the _ clone () method of php? What are their meanings?
Class Person {// The following is the member attribute var $ name of a Person; // The name of a Person var $ sex; // gender var $ age of a Person; // person's age // 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;} // The method that this person can talk to and state his/her attribute 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. in this way, the attributes of the copy are changed. $ This-> name = "I am copying Michael Jacob $ this-> name"; // $ this-> age = 30 ;}} $ p1 = new Person ("zhang san", "male", 20); $ p2 = clone $ p1; $ p1-> say (); $ p2-> say ();
Reply content:
How to explain the two $ this in the _ clone () method of php? What are their meanings?
Class Person {// The following is the member attribute var $ name of a Person; // The name of a Person var $ sex; // gender var $ age of a Person; // person's age // 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;} // The method that this person can talk to and state his/her attribute 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. in this way, the attributes of the copy are changed. $ This-> name = "I am copying Michael Jacob $ this-> name"; // $ this-> age = 30 ;}} $ p1 = new Person ("zhang san", "male", 20); $ p2 = clone $ p1; $ p1-> say (); $ p2-> say ();
__clone()
No two$this
, Only one$this
, This$this
The point is the cloned new object, because__clone()
The method is called in the new object.
During cloning, PHP will first perform a light copy to create an object and copy all the attributes of the original object to the new object. For reference variables such as objects and resources, just copy their pointers without cloning them. If you need to perform deep replication on these attributes, you must__clone()
Clone it separately.
For example:
class MyCloneable{ public $obj; function __clone() { $this->obj = clone $this->obj; }}
Note:$this->obj
All refer to the new class$obj
Attribute, because the new object$obj
The original object has been shorily copied. this is because we want to perform deep replication$this->obj
Then, a clone is executed.