PHP lazy Static binding: Refers to the self of the class, not as defined, but rather as the result of the operation of the calculation.
(1) When a subclass instantiates an object $stu calls the Say method, it runs within the parent class human, so Self::hei () in Say () is the HEI () method that invokes the parent class.
(2) Static:: Method Name (): Use the static keyword, the first is to find the method in the subclass, if not found, then find in the parent class.
Usage Scenarios
First look at the following code:
Abstract class Base { //do sth}class AClass extends base{public static function create () { return new AClass () ; } }class Bclass extends base{public static function create () { return new Bclass (); }} Var_dump (Aclass::create ()); Var_dump (Bclass::create ());
Output:
Object (aclass) #1 (0) {} object (Bclass) #1 (0) {}
The above AClass and bclass inherit from the abstract class base, but the static method of Create () is implemented at the same time in two sub-classes. In accordance with the OOP idea, this duplication of code should be implemented in the parent class of base.
Improved code
Abstract class Base {public static function create () { return to new self (); }} Class AClass extends Base{}class Bclass extends Base{}var_dump (Aclass::create ()); Var_dump (Bclass::create ());
Now that the code seems to fit our previous idea of sharing the Create () method in the parent class, let's run it and see what happens.
Cannot instantiate abstract class base in ...
Unfortunately, the code does not seem to run as we expected, and self () in the parent class is resolved to the parent class of base, not inheriting from his subclass. In order to solve this problem, the concept of delayed static binding is introduced in php5.3.
Lazy static binding
Abstract class Base {public static function create () { return new static ()} } Class AClass extends Base{}class Bclass extends Base{}var_dump (Aclass::create ()); Var_dump (Bclass::create ());
This code is almost identical to the previous one, the difference is that the self is replaced by the static keyword, static will resolve to the subclass, not the parent class, this can solve the problem encountered above, this is the lazy static binding PHP.
Finally, run the code and get the results you want.
object (aclass) #1 (0) {} object (Bclass) #1 (0) {}