Home > Article > Backend Development > PHP delayed static binding instance analysis, delayed static instance analysis_PHP tutorial
The example in this article describes the method of delayed static binding in PHP. Share it with everyone for your reference. The specific analysis is as follows:
php delayed static binding: refers to the self of the class, which is not based on the time of definition, but based on the running results during calculation. Let’s look at an example first
<?php header("content-type:text/html;charset=utf-8"); class Human{ public static function hei(){ echo "我是父类的hei()方法"; } public function say(){//如果子类调用父类的say()方法,则 self::hei();//这里调用的是父类的hei()方法 static::hei(); //这里调用子类的hei()方法,如果子类不存在hei()方法,则调用父类的 } } class Stu extends Human{ public static function hei(){ echo "我是子类的hei()方法"; } } $stu = new Stu(); $stu->say(); ?>
Description:
(1) When the subclass instantiated object $stu calls the say method, it runs within the parent class Human. Therefore, self::hei() in say() calls the hei() method of the parent class.
(2) static::method name(): If you use the static keyword, you will first search for the method in the subclass; if it cannot be found, search it in the parent class.
I hope this article will be helpful to everyone’s PHP programming design.