Home >Backend Development >PHP Tutorial >PHP delayed static binding instance analysis, delayed static instance analysis_PHP tutorial

PHP delayed static binding instance analysis, delayed static instance analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:07:25817browse

php delayed static binding instance analysis, delayed static instance analysis

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

<&#63;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();
&#63;>

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955409.htmlTechArticlephp delayed static binding instance analysis, delayed static instance analysis 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 delay static...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn