Home  >  Article  >  Backend Development  >  PHP delayed static binding example analysis_PHP tutorial

PHP delayed static binding example analysis_PHP tutorial

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

PHP delayed static binding example analysis

This article mainly introduces PHP delayed static binding. The example analyzes the principle and implementation skills of delayed static binding. Friends who need it You can refer to the following

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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();

?>

1 2

3

4

5

6

7

8 9

1011 12 13 14 15 16
17
18
19 20 21
<🎜>header("content-type:text/html;charset=utf-8");<🎜> <🎜>class Human{<🎜> <🎜>public static function hei(){<🎜> <🎜>echo "I am the hei() method of the parent class";<🎜> <🎜>}<🎜> <🎜>public function say(){//If the subclass calls the say() method of the parent class, then <🎜> <🎜>self::hei();//What is called here is the hei() method of the parent class<🎜> <🎜>static::hei();<🎜> <🎜>//The hei() method of the subclass is called here. If the hei() method does not exist in the subclass, the <🎜> of the parent class is called. <🎜>}<🎜> <🎜>}<🎜> <🎜>class Stu extends Human{<🎜> <🎜>public static function hei(){<🎜> <🎜>echo "I am the hei() method of the subclass";<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>$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. http://www.bkjia.com/PHPjc/955271.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955271.htmlTechArticlephp delayed static binding example analysis This article mainly introduces php delayed static binding, and the example analysis of delayed static binding For the principles and implementation techniques of binding, friends in need can refer to the examples in this article...
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