This, parent, and self are all in the class in php. Next I will introduce this, parent and self respectively. For more information about the application and difference in the class, see. This, parent, and self are all in the class in php. Next I will introduce this, parent and self respectively. For more information about the application and difference in the class, see.
Script ec (2); script
1, this
1. to use this, you must be in a situation like this. Otherwise, it will report an error: Fatal error: Using $ this when not in object context.
2. this can call methods and attributes in this class, or call methods and attributes that can be called in the parent class.
| The Code is as follows: |
|
|
<? Php
Class UserName
{
// Define attributes
Private $ name;
// Define the constructor
Function _ construct ($ name)
{
$ This-> name = $ name;
// This pointer has been used here
}
// Destructor
Function _ destruct (){}
// Print the username member function
Function printName ()
{
Print ($ this-> name );
// The PHP keyword this pointer is used again.
}
}
// Instantiate the object
$ NameObject = new UserName
("Heiyeluren ");
// Execute print
$ NameObject-> printName ();
// Output: heiyeluren
// The second Object Instantiation
$ NameObject2 = new UserName ("PHP5 ");
// Execute print
$ NameObject2-> printName (); // output: PHP5
?> |
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, then print ($ this-> Name), then of course the output is "heiyeluren ".
Ii. self
Self points to the class itself, that is, the PHP self keyword does not point to any instantiated object. Generally, self points to static variables in the class.
1. self can access static attributes and static methods in this class, and can access static attributes and static methods in the parent class.
2. When using self, you do not need to instantiate
| The Code is as follows: |
|
|
<? Php
Class Counter
{
// Define attributes, including a static variable
Private static $ firstCount = 0;
Private $ lastCount;
// Constructor
Function _ construct ()
{
$ This-> lastCount = ++ selft
: $ FirstCount;
// Use the PHP self keyword to call static variables and use self
The call must use: (domain operator number)
}
// Print the maximum value
Function printLastCount ()
{
Print ($ this-> lastCount );
}
}
// Instantiate the object
$ CountObject = new Counter ();
$ CountObject-> printLastCount ();
// Output 1
?> |
3. parent
Parent: can be used to call the member methods defined in the parent class.
Parent: tracing is not only a direct parent class.
Use parent: to call the parent class Method
| The Code is as follows: |
|
|
Class employee {
Protected $ sal = 3000;
Public function getSal (){
$ This-> sal = $ this-> sal + 1200;
Return $ this-> sal;
}
}
Class Manager extends employee {
// If you want the manager to pay more than 1500 yuan on the employee's salary.
// The getSal () method of the parent class must be called first.
Public function getSal (){
Parent: getSal (); // The method of the parent class is called here.
$ This-> sal = $ this-> sal + 1500;
Return $ this-> sal;
}
}
$ Emp = new employee ();
Echo "the average employee's salary is". $ emp-> getSal ();
Echo"
";
$ Manager = new Manager ();
Echo "the manager's salary is:". $ manager-> getSal ();
?> |
Parent is a pointer to the parent class. Generally, we use parent to call the constructor of the parent class.