Home > Article > Backend Development > PHP Static delayed static binding usage analysis_php skills
The example in this article describes the usage of PHP Static delayed static binding. Share it with everyone for your reference, the details are as follows:
After PHP5.3, delayed static binding static was introduced. What problem is it intended to solve? A long-standing problem in PHP's inheritance model is that it is difficult to reference the final state of an extended class in the parent class. Let’s look at an example.
class A { public static function echoClass(){ echo __CLASS__; } public static function test(){ self::echoClass(); } } class B extends A { public static function echoClass() { echo __CLASS__; } } B::test(); //输出A
A new feature has been added to PHP5.3: delayed static binding, which means that expressions or variables that were originally fixed in the definition phase are changed to be determined in the execution phase. For example, when a subclass inherits a parent class When it is a static expression, its value cannot be changed. Sometimes you don't want to see this situation.
The following example solves the problem raised above:
class A { public static function echoClass(){ echo __CLASS__; } public static function test() { static::echoClass(); } } class B extends A { public static function echoClass(){ echo __CLASS__; } } B::test(); //输出B
static::echoClass(); on line 8 defines a static delayed binding method. The method that was originally defined will not be executed until B calls test.
Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP operating office document skills (including word, excel, access, ppt)", "php date Time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation Introductory tutorial " and " Summary of common PHP database operation skills "
I hope this article will be helpful to everyone in PHP programming.