This, self, and parent keywords in PHP5 _ PHP Tutorial

Source: Internet
Author: User
This, self, and parent keywords in PHP5 are described in detail. Php (as the mainstream development language) 5 is a language with most of the object-oriented language features, compared with php (as the mainstream development language) 4. with a lot of object-oriented features, php (as the mainstream development language) 5 is a language with the features of most object-oriented languages, compared with php (as the mainstream development language) 4, php has many object-oriented features, but some concepts are also quite confusing. so today I am talking about it and it is not good, please forgive me. (reading this article, you need to understand php (as the mainstream development language) 5 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 ("php (as the mainstream development language) 5 ");
32
33 // Print
34 $ nameObject2-> printName (); // output: php (as the mainstream development language) 5
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 "php (as the mainstream development language) is output) 5 ". 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 ()

Http://www.bkjia.com/PHPjc/508642.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508642.htmlTechArticlephp (as the mainstream development language) 5 is a language with most of the characteristics of object-oriented language, than php (as the mainstream development language) 4. a lot of object-oriented features...

Related Article

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.