This, self, and parent keywords in PHP5

Source: Internet
Author: User
This, self, and parent keywords in PHP5 are described in detail. Read the details of this, self, and parent keywords in PHP5. PHP5 is a language with most of the characteristics of object-oriented languages, PHP4 has a lot of object-oriented features, but some concepts are also quite confusing. so let's talk about it today. sorry. (Read this article to learn more about PHP5 "> <LINKh

PHP5 is a language that has the characteristics of most object-oriented languages. it has many object-oriented features than PHP4, but some concepts are also quite confusing. so let's talk about it today, sorry. (Read this article to learn about PHP5's object-oriented knowledge)

First, let's understand the above three keywords: this, self, and parent, which are literally easy to understand. it means this: yourself, father, haha, it's fun, let's first establish several concepts. where are these three keywords used? This is a pointer to the current object (let's look at the pointer in C), self is a pointer to the current class, and parent is a 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 2
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 row 11 and row 20 respectively, so 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, then print ($ this-> Name), then of course the output is "heiyeluren ". 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 variable is called in 12 rows. self is used to call the variable 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 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 all public, especially 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!

My mailbox: heiyeluren@163.com

WriteTime:

Http://dev.csdn.net/author/heiyeshuwu/702e33d6abaf4be58c06f1b55cf0fc8c.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.