One, this
1, to use this, you have to be a situation of the image, otherwise it will be an error, Fatal error:using $this when not in the object context.
2,this can call methods and properties in this class, or you can call methods and properties that are adjustable in the parent class
| The code is as follows |
Copy Code |
|
<?php
Class username
{
//Defining Properties br> private $name;
//definition constructor
function __construct ($name)
{ ;
$this->name = $name;
//This pointer is already used here
}
//destructor
Function __destruct () {} & nbsp
//Print User name member function
Function printname ()
{
Print ($this->name);
//also uses the PHP keyword this pointer
}
}
//instantiated object
$ Nameobject = new UserName
("Heiyeluren");
//Perform print
$nameObject->printname ();
//Output: heiyeluren
//Second instantiation object
$nameObject 2 = new UserName ("PHP5"); &nbs P
//execute print
$nameObject 2->printname ()//Output:php5
? >
|
Let's see, the class above uses the this pointer on lines 11 and 20 respectively, so who is this pointing to? In fact, this is when the instantiation to determine who to point to, such as the first instantiation of the object (25 lines), then this is to point to the $nameobject object, then perform 18 lines of printing when the print ($this-><name) becomes print ($nameObject->name), then of course the "Heiyeluren" is exported.
Two, self
Self is pointing to the class itself, that is, the PHP self keyword does not point to any object that has already been instantiated, and general self uses it to point to static variables in the class.
1,self can access static and static methods in this class, and you can access static properties and static methods in the parent class.
2, when you use self, you can not instantiate the
| The code is as follows |
Copy Code |
|
< PHP
Class Counter
{
Define attributes, including a static variable
private static $firstCount = 0;
Private $lastCount;
Constructors
function __construct ()
{
$this->lastcount = ++selft
:: $firstCount;
Use the PHP self keyword to invoke a static variable, using the self
Call must use::(field operator symbol)
}
Print the maximum number of times
function Printlastcount ()
{
Print ($this->lastcount);
}
}
Instantiating an Object
$countObject = new Counter ();
$countObject->printlastcount ();
Output 1
?> |
Third, parent
Parent:: Can be used to invoke the member method defined in the parent class.
Parent:: The retrospective is not only in the direct parent class.
By Parent:: Calling the method of the parents class
| code is as follows |
copy code |
|
<!--declare an employee class, the manager class inherits from the employee class-->
?
Class employee{
protected $sal = 3000;
Public Function Getsal () {
$this->sal = $this->sal + 1200;
return $this->sal;
}
}
Class Manager extends Employee {
If you want to get the manager to pay more than 1500 yuan on the basis of employee's salary.
The Getsal () method of the parent class must be called first.
Public Function Getsal () {
Parent::getsal ();//This invokes the method of the parent class.
$this->sal = $this->sal + 1500;
return $this->sal;
}
}
$emp = new Employee ();
echo "The wages of ordinary employees is". $emp->getsal ();
echo "<br>";
$manager = new manager ();
echo "Manager's salary is:". $manager->getsal ();
?> |
Parent is a pointer to a parent class, in general we use parent to invoke the constructor of the parent class.