PHP delayed static binding example analysis

高洛峰
Release: 2023-03-04 10:48:01
Original
1051 people have browsed it

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();
?>
Copy after login

Description:

(1) When the subclass instantiated object $stu calls the say method, it runs in the parent class Human, so , 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.

For more articles related to PHP delayed static binding example analysis, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!