Home  >  Article  >  Backend Development  >  PHP late static binding example sharing

PHP late static binding example sharing

小云云
小云云Original
2018-03-13 11:47:371167browse

1. The working principle of late static binding is to store the class name in the previous "non-forwarding call" (non-forwarding call). When making a static method call, the class name is the one explicitly specified (usually on the left side of the :: operator); when making a non-static method call, it is the class to which the object belongs. This feature is named "late static binding" from a language-internal perspective. "Late binding" means that static:: is no longer resolved to the class in which the current method is defined, but is calculated at actual runtime.

2. Test example:

class A{    public function run()    
{        static::test(); //后期静态绑定        
self::test(); //不是后期静态绑定    }    
public static function test()    
{        echo &#39;A Class<br>&#39;;    
}}class B extends A
{    public static function test()   
{        echo &#39;B Class<br>&#39;;    }}
$a = new B();$a->run();
//输出结果
//B Class
//A Class

Related recommendations:

Detailed description of a late static binding in Laravel

PHP object-oriented post-static binding function introduction

php post-static binding

The above is the detailed content of PHP late static binding example sharing. For more information, please follow other related articles on the PHP Chinese website!

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