PHP5 pointer category introduction. We also understand many of the three keywords: this, self, and parent, which are literally easy to understand. it means that it is quite fun to know yourself, father, and haha, let's first establish several concepts.First of all, let's understand three keywords: this, self, and parent, which are literally easy to understand. this refers to myself, father, haha. it's quite 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.
If we do not know much about this, let's talk about it based on the actual examples.
(1) PHP5 pointer to this
-
Php
-
- Class UserName
- {
- // Define attributes
- Private $ name;
-
- // Define the constructor
- Function _ construct ($ name)
- {
- $ This->Name= $ Name; // this pointer is already used here
- }
-
- // Destructor
- Function _ destruct (){}
-
- // Print the username member function
- Function printName ()
- {
- Print ($ this->Name); // this pointer is used again
- }
- }
-
- // Instantiate the object
- $NameObject=NewUserName ("heiyeluren ");
-
- // Execute print
- $ NameObject->PrintName (); // output: heiyeluren
-
- // The second object instantiation
- $NameObject2=NewUserName ("PHP5 ");
-
- // Execute print
- $ NameObject2->PrintName (); // output: PHP5
- ?>
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) PHP5 pointer 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.
-
Php
-
- Class Counter
- {
- // Define attributes, including a static variable
- Private static $FirstCount=0;
- Private $ lastCount;
- // Constructor
- Function _ construct ()
- {
- $ This->LastCount= ++ Selft: $ firstCount; // Use self to call static variables. the following must be used to call static variables: (domain operator number)
- }
- // Print the maximum value
- Function printLastCount ()
- {
- Print ($ this->LastCount );
- }
- }
-
- // Instantiate the object
- $CountObject=NewCounter ();
- $ CountObject->PrintLastCount (); // output 1
- ?>
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, when the value is called in 12 rows, we use self to call the variable, and we use ":" In the middle to connect, which 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 instance of the following object. they are only related to the class. so if I call the class itself, we cannot use this for reference. 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 of PHP5 pointer
We know that parent is a pointer to the parent class. generally, we use parent to call the constructor of the parent class.
-
Php
- // Base class
- Class Animal
- {
- // Attributes of the base class
- Public $ name; // name
- // Constructor of the base class
- Public function _ construct ($ name)
- {
- $ This->Name= $ Name;
- }
- }
- // Derived class
- Class Person extends Animal // Person class inherits Animal class
- {
- Public $ personSex; // gender
- Public $ personAge; // age
- // Constructor of the inherited class
- Function _ construct ($ personSex, $ personAge)
- {
- Parent: :__ construct ("heiyeluren"); // uses parent to call the constructor of the parent class.
- $ This->PersonSex= $ PersonSex;
- $ This->PersonAge= $ PersonAge;
- }
- Function printPerson ()
- {
- Print ($ this->Name. "is". $ this->PersonSex. ", this year". $ this->PersonAge );
- }
- }
- // Instantiate the Person object
- $PersonObject=NewPerson ("male", "21 ");
-
- // Execute print
- $ PersonObject->PrintPerson (); // output: heiyeluren is male, this year 21
-
- ?>
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. Note the key points. row 25th: parent: _ construct ("heiyeluren "), at this time, we will use the parent in the PHP5 pointer to call the constructor of the parent class to initialize the parent class, because the members of the parent class are all public, therefore, we can directly use this to call the inheritance class.
First of all, let's understand three keywords: this, self, parent, which is literally easy to understand. it means that it's fun to be yourself, father, haha, let's first establish several concepts, these three...