PHP object-oriented static binding function introduction and object-oriented function introduction. PHP object-oriented static binding later. This article will introduce PHP static binding later, it is mainly used to introduce the static binding feature of PHP Objects referenced in the inheritance scope. It also introduces the object-oriented feature.
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 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 problem of referencing static binding within the inheritance scope...