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.
This:
<? PHPClass 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 = new username ("heiyeluren ");
// Execute print
$ Nameobject-> printname (); // output: heiyeluren
// The second Object Instantiation
$ Nameobject = new username ("php ");
// Execute print
$ Nameobject-> printname (); // output: PHP
?>
Note: This cannot reference static variables. This is to point to the current object.
SELF:
<? PHPClass counter
{
// Define attributes, including a static variable
Private Static $ firstcount =;
Private $ lastcount;
// Constructor
Function _ construct ()
{
$ This-> lastcount = ++ selft: $ firstcount; // use self to call static variables. To use self to call static variables, you must use: (domain operator number)
}
// Print the maximum value
Function printlastcount ()
{
Print ($ this-> lastcount );
}
}
// Instantiate the object
$ Countobject = new counter ();
$ Countobject-> printlastcount (); // output
?>
Note: Self points to the class itself, that is, self does not point to any instantiated object. Generally, self points to static variables and static methods in the class.
Whenever self uses the domain operator: static variables used by the called static variables or static methods must be initialized; otherwise, the variable is not initialized.
Note that the call to static variables must contain $
Parent:
<? 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 = new person ("male ","");
// Execute print
$ Personobject-> printperson (); // output: heiyeluren is male, this year
?>
We know that parent is a pointer to the parent class. Generally, we use parent to call the constructor and method of the parent class. It also uses the domain operator: Call, and cannot call attributes.