This article will introduce PHP's late static binding function, which is primarily used to resolve classes that reference static calls within the inheritance scope.
First 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, because self:: Depends on the class in which it is defined, not the class in which it is running. To solve this problem, you might rewrite the status () method in an inherited class, and a better solution would be to add later static binding functionality after PHP 5.3.
Copy code code 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
Visible, Static:: Does not point to the current class, in fact, it is computed in run, forcing to get all the properties of the final class.
Therefore, it is recommended that you do not use self again::, using static::