THIS, SELF and PARENT keywords in PHP5

xiaoxiao2021-03-06  91

THIS, SELF and PARENT keywords in PHP5

Text / Heiyeluren2004 / 11/03

PHP5 is a language with most of the object-oriented language. It has a lot of object-oriented features than php4, but some concepts are more around, so today I will talk about it, please forgive me. (Read this article, you need to understand the object-oriented knowledge of php5)

First let's understand the above three keywords: this, self, parent, from the literally understood, refers to this, you, my father, huh, it is more fun, let's build a few concepts first, these three keywords respectively Where is it used? We initially explained that this is a pointer to the current object (we look at the pointer in C), and Self is a pointer to the current class, and the Parent is a pointer to the parent class. We often use pointers here to describe because there is no better language to express, huh, Chinese, have not learned. -_- #

So can't understand very well, then we will talk about the actual example.

(1) THIS

1 name = $ name; // This has been used here Pointer 12} 13 14 // Destructor 15 function __destruct () {} 1617 // Print User Name Member Function 18 Function PrintName () 19 {20 Print ($ this-> name); // Use this pointer 21 } 22} 2324 // Instantiation Object 25 $ NameObject = New UserName ("Heiyeluren"); 2627 // Performs Print 28 $ NameObject-> PrintName (); // Out: Heiyeluren29 30 // Second Instantiation Object 31 $ nameObject2 = new username ("php5"); 32 33 // Perform print 34 $ NameObject2-> printname (); // Output: php535?>

We look, the above class uses the THIS pointer in 11 rows and 20, then why is this THIS? In fact, this is when instantiating to determine who is pointing to who, such as the first instantiation of objects (25 lines), then this is to point to $ nameObject object, then implement the 18 line print, put the print ($ this -> name), then "Heiyeluren" is Of course. In the second example, Print ($ this-> name) became Print ($ nameObject2-> name), and "php5" is output. So, this is a pointer to the current object instance, does not point to any other object or class.

(2) Self

First we have to clarify it, Self is the class itself, that is, SELF does not point to any instantiated object, usually Self use to point to static variables in the class.

1 lastcount = Selft :: $ firstcount; // Use Self to call static variables, use the SELF call must be used:: (domain arithmetic symbol) 13} 1415 // Print the most value 16 function printlastcount () 17 {18 Print ($ this- > lastcount); 19} 20} 2122 // Instantiated object 23 $ countObject = new counter (); 2425 $ countObject-> printlastCount (); // Output 12627?> We just pay attention to two places, line 6 and Chapter 12. We defined a static variable $ firstcount in the second line, and the initial value was 0, then call this worth in 12 lines, use Self to call, and use "::" to connect, it is our so-called The domain operator, then this time we call the static variable $ FRESTCOUNT, the static variable is not related to the instance of the object below, it is just related to the class, then I call the class itself, then we can't Use this to reference, you can use Self to reference because SELF is a point to the class itself, independent of any object instance. In other words, if the static member in our class, we must also use Self to call.

(3) Parent

We know that Parent is a pointer to the parent class, usually we use Parent to call the parent class constructor.

1 Name = $ name; 13} 14} 1516 // Derive class 17 Class Person Extends Animal // Person class inherits animal class 18 {19 public $ personsex; // Gender 20 public $ personage; // age 2122 // Inherited class Constructor 23 function __construct ($ Personsex, $ Personage) 24 {25 Parent :: __ Construct ("Heiyeluren"); // Using Parent calls the constructor of the parent class 26 $ this-> Personsex = $ PERSONSEX; 27 $ THIS -> Personage = $ Personage; 28} 2930 function printperson () 31 {32 Print ($ this-> name. "IS". $ this-> Personsex. ", this year". $ this-> personage); 33} 34} 3536 // Installation Person object 37 $ PERSONOBJECT = New Person ("Male", "21"); 3839 // Perform print 40 $ PERSONOBJECT-> PrintPerson (); // Output: Heiyeluren Is Male, this year 214142 ?> We note such a few details: member properties are public, especially the parent class, is to access the inheritance class to access. We pay attention to the key place, chapter 25: Parent :: __ Construct ("heiyeluren"), then we use the Parent to call the parent class's constructor to initialize the parent class, because the members of the parent class are public, So we can call them directly in the inheritance class.

to sum up:

This is a pointer to the object instance, and Self is a reference to the class itself, and the Parent is a reference to the parent class.

Basically, I know so much, there is definitely something wrong, please point out!

My email: heiyeluren@163.com

Writetime: 2004-11-3 18:30

转载请注明原文地址:https://www.9cbs.com/read-96733.html

New Post(0)