| PhP5 is a language that has the characteristics of most object-oriented languages. PhP4 has many object-oriented features, but some concepts are hard to understand, here I mainly talk about the differences between the three keywords "this", "self", and "parent. Literally, it means this, yourself, and your father. Let's first establish several concepts. Where are these three keywords used? We will give a preliminary explanation. This is the pointer to the current object (let's take a look at the pointer in c). self is the pointer to the current class, and parent is the pointer to the parent class. We often use pointers to describe them here because there is no better language to express them.
If we do not know much about this, let's talk about it based on the actual examples.
(1) This
1 <? PHP
3 class Username
4 {
5 // define 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 first instantiated (25 rows), then this is to point to the $ nameobject object, when printing 18 rows, the print ($ this-> <name) is changed to the print ($ nameobject-> name), and the "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 <? PHP
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 is our so-called domain operator. At this time, we call the static variable $ frestcount defined by the class. Our static variables have nothing to do with the instances of the following objects, it is only related to the class, so I call the class itself, so we cannot use this for reference, we can use self for reference, because self is pointing to the class itself, it is irrelevant to 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 <? PHP
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.
Basically, I know so much about it, and there must be some misunderstanding. Please point it out! |