PHP Object-Oriented Introduction to static binding later. PHP object-oriented static binding later this article will introduce PHP static binding later, it is mainly used to solve the class that references static calls within the inheritance scope. First, let's take a look at the PHP Object-Oriented Introduction to the later static binding function
This article will introduce PHP static binding later, which is mainly used to solve the class that references static calls within the inheritance scope.
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, because self: depends on the class in the definition, rather than the class in the running. To solve this problem, you may rewrite the status () method in the inheritance class. a better solution is to add the static binding function after PHP 5.3.
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: does not point to the current class. In fact, it is calculated during running, and all attributes of the final class are forcibly obtained.
Therefore, we recommend that you do not use self: or static: In the future ::
In this article, we will introduce the static binding function of PHP later. it is mainly used to solve the class that references static calls within the inheritance scope. First let's take a look...