: This article mainly introduces the distinction and comparison of the three keywords "this", "self", and "parent" in php. if you are interested in PHP tutorials, refer to them. The three keywords "this, self, and parent" are literally easy to understand, namely, "yourself" and "father.
This is the pointer to the current object (let's look at it using the pointer in C)
Self is a pointer to the current class.
Parent is a pointer to the parent class (we often use pointers to describe it here because there is no better language to express it)
Let's take a look at the actual example.
(1) this
1 2
3 class UserName
4 {
5 // define member attributes
6 private $ name;
7
8 // define the constructor
9 function _ construct ($ name)
10 {
11 $ this-> name = $ name; // this pointer is already used here
12}
13
14 // destructor
15 function _ destruct (){}
16
17 // Print the username member function
18 function printName ()
19 {
20 print ($ this-> name); // this pointer is used again
21}
22}
23
24 // instantiate the object
25 $ nameObject = new UserName ("heiyeluren ");
26
27 // execute print
28 $ nameObject-> printName (); // output: heiyeluren
29
30 // second object instantiation
31 $ nameObject2 = new UserName ("PHP5 ");
32
33 // Print
34 $ nameObject2-> printName (); // output: PHP5
35?>
We can see that the class above uses the this pointer in 11 rows and 20 rows respectively. Who is this pointing?
In fact, this is used to determine who to point to During instantiation. for example, when the object is instantiated for the first time (25 rows ),
At that time, this is the $ nameObject Object. when printing 18 rows, print ($ this-> name) is changed to print ($ nameObject-> name ), of course, "heiyeluren" is output ".
In the second instance, print ($ this-> name) is changed to print ($ nameObject2-> name), so "PHP5" is output ".
Therefore, this is a pointer to the current object instance and does not point to any other object or class.
(2) self
First, we need to make it clear that self points to the class itself, that is, self does not point to any instantiated object. Generally, self points to static variables in the class.
1 2
3 class Counter
4 {
5 // define attributes, including a static variable
6 private static $ firstCount = 0;
7 private $ lastCount;
8
9 // Constructor
10 function _ construct ()
11 {
12 $ this-> lastCount = ++ selft: $ firstCount; // Use self to call static variables. to use self to call static variables, you must use: (domain operator number)
13}
14
15 // Print the maximum value
16 function printLastCount ()
17 {
18 print ($ this-> lastCount );
19}
20}
21
22 // instantiate the object
23 $ countObject = new Counter ();
24
25 $ countObject-> printLastCount (); // output 1
26
27?>
Here, we only need to pay attention to two places: 6th rows and 12th rows.
We defined a static variable $ firstCount in the second row, and the initial value is 0. Therefore, this value is called in 12 rows using self and ":: "To connect,
This is our so-called domain operator. at this time, we call the static variable $ frestCount defined by the class. our static variable is irrelevant to the instance of the following object. it is only related to the class,
Then, if I call the class itself, we cannot use this for reference, and we can use self for reference,
Because self points to the class itself and has nothing to do with any object instance. In other words, we must also use self to call static members in our class.
(3) parent
We know that parent is a pointer to the parent class. generally, we use parent to call the constructor of the parent class.
1 2
3 // base class
4 class Animal
5 {
6 // attributes of the base class
7 public $ name; // name
8
9 // Constructor of the base class
10 public function _ construct ($ name)
11 {
12 $ this-> name = $ name;
13}
14}
15
16 // derived class
17 class Person extends Animal // Person class inherits Animal class
18 {
19 public $ personSex; // gender
20 public $ personAge; // age
21
22 // Constructor of the inherited class
23 function _ construct ($ personSex, $ personAge)
24 {
25 parent ::__ construct ("heiyeluren"); // uses parent to call the constructor of the parent class
26 $ this-> personSex = $ personSex;
27 $ this-> personAge = $ personAge;
28}
29
30 function printPerson ()
31 {
32 print ($ this-> name. "is". $ this-> personSex. ", this year". $ this-> personAge );
33}
34}
35
36 // instantiate the Person object
37 $ personObject = new Person ("male", "21 ");
38
39 // Print
40 $ personObject-> printPerson (); // output: heiyeluren is male, this year 21
41
42?>
We should pay attention to the following details: the member attributes are public, especially for the parent class, to be accessed by the inheritance class through this.
We should pay attention to the key points: row 25th: parent ::__ construct ("heiyeluren"). at this time, we will use parent to call the constructor of the parent class to initialize the parent class,
Because the members of the parent class are all public, we can directly use this to call the class in the inheritance class.
Summary:
This is a pointer to the object instance. self is a reference to the class itself, and parent is a reference to the parent class.
Reprinted from: http://blog.csdn.net/skynet001/article/details/7518164
The preceding section describes how to differentiate and compare the three keywords "this", "self", and "parent" in php, including the following content. I hope to help those who are interested in the PHP Tutorial.