Home  >  Article  >  Backend Development  >  Detailed explanation of late static binding in PHP object-oriented

Detailed explanation of late static binding in PHP object-oriented

藏色散人
藏色散人forward
2021-01-18 14:57:143044browse

Recommended: "PHP Video Tutorial"

Introduction

Since PHP 5.3.0, PHP has added a feature called The function of late static binding is used to reference statically called classes in the inheritance scope.

To be precise, 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. The so-called "forwarding call" refers to static calls in the following ways: self::, parent::, static:: and forward_static_call(). You can use the get_called_class() function to get the class name of the called method, and static:: points out its scope.

Forwarded calls & non-forwarded calls

Forwarded calls

The so-called forwarded calls refer to: method calls in a class Forwarding callInformation(can be understood as call stack information)

  • Timing of forwarding call

    • parent call

    • self call

    • static call

    • forward_static_call()

      Non-forwarded call

      The so-called non-forwarded call refers to: a class name that explicitly specifies a static call or a call through an object (pseudo-object) instance

      Code Analysis

      <?phpclass A{public static function foo(){
        static::who();}public static function who(){
        echo __CLASS__ . "\n";}}class B extends A{public static function test(){
        //非转发调用
        A::foo();
        //转发调用
        parent::foo();
        //转发调用
        self::foo();}public static function who(){
        echo __CLASS__ . "\n";}}class C extends B{public static function who(){
        echo __CLASS__ . "\n";}}//非转发调用C::test();

      Late Static Binding

      This function 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. It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.

      Implementation principle of late static binding

      The working principle of late static binding is to store the class name in the previous "non-forwarding call" .
      The meaning of this sentence is how to confirm which class static is during the running of the program. The determination rule is to take the last stored non-forwarded call class

      Later static binding Customized application

  • Single instance inheritance scenario

  • Abstract class and implementation class

The above is the detailed content of Detailed explanation of late static binding in PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete