This article will introduce the post-static binding function of PHP. It is mainly used to solve the problem of referencing static calls in the inheritance scope. kind.
First, let’s look at the following example:
The code is as follows:
class Person
{
public static function status()
{
Self::getStatus();
}
protected static function getStatus()
{
echo "Person is alive";
}
}
class Deceased extends Person
{
protected static function getStatus()
{
echo "Person is deceased";
}
}
Deceased::status(); //Person is alive
Obviously, the result is not what we expected. This is because self:: depends on the class in which it is defined, not the running class. In order to solve this problem, you may override the status() method in the inherited class. A better solution is that PHP 5.3 added the function of late static binding.
Copy the code. The code is as follows:
class Person
{
public static function status()
{
static::getStatus();
}
protected static function getStatus()
{
echo "Person is alive";
}
}
class Deceased extends Person
{
protected static function getStatus()
{
echo "Person is deceased";
}
}
Deceased::status(); //Person is deceased
It can be seen that static:: no longer points to the current class. In fact, it is calculated at runtime, forcing all properties of the final class to be obtained.
Therefore, it is recommended not to use self:: in the future, but to use static::