zoukankan      html  css  js  c++  java
  • php Late Static Bindings延迟静态绑定

    官网说道:

    As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance.

    More precisely, late static bindings work by storing the class named in the last "non-forwarding call". In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non static method calls, it is the class of the object. A "forwarding call" is a static one that is introduced by self::,parent::static::, or, if going up in the class hierarchy, forward_static_call(). The function get_called_class() can be used to retrieve a string with the name of the called class and static:: introduces its scope.

    This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls.

     

    Limitations of self:: ¶

    Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined:

    Example #1 self:: usage、

    class A {
        public static function who() {
            echo __CLASS__;
        }
        public static function test() {
            self::who();
        }
    }
    
    class B extends A {
        public static function who() {
            echo __CLASS__;
        }
    }
    
    B::test();

    输出A。

    Late Static Bindings' usage ¶

    Late static bindings tries to solve that limitation by introducing a keyword that references the class that was initially called at runtime. Basically, a keyword that would allow you to reference B from test() in the previous example. It was decided not to introduce a new keyword but rather use static that was already reserved.

    Example #2 static:: simple usage

    <?php
    class A {
        public static function who() {
            echo __CLASS__;
        }
        public static function test() {
            static::who(); // Here comes Late Static Bindings
        }
    }
    
    class B extends A {
        public static function who() {
            echo __CLASS__;
        }
    }
    
    B::test();
    ?>

    输出B;

    Note:

    In non-static contexts, the called class will be the class of the object instance. Since $this-> will try to call private methods from the same scope, using static:: may give different results. Another difference is thatstatic:: can only refer to static properties.

    Example #3 static:: usage in a non-static context

    class A {
        private function foo() {
            echo "success!
    ";
        }
        public function test() {
            $this->foo();
            static::foo();
        }
    }
    
    class B extends A {
       /* foo() will be copied to B, hence its scope will still be A and
        * the call be successful */
    }
    
    class C extends A {
        private function foo() {
            /* original method is replaced; the scope of the new one is C */
        }
    }
    
    $b = new B();
    $b->test();
    $c = new C();
    $c->test();   //fails

    success! success! success!
    ( ! ) Fatal error: Call to private method C::foo() from context 'A' in F:xampphtdocsphpphpSyntaxlateStaticBinding.php on line 26

    更多:

    http://cn2.php.net/lsb

  • 相关阅读:
    绘制文字时如何让文字居中显示
    python获取指定时间段内的随机不重复的时间点
    python时间时分秒与秒数的互相转换
    python获取字母在字母表对应位置的几种方法及性能对比较
    配置指定使用tcc编译器编译nim程序
    nim也玩一行流,nim版的list comprehension
    python 中x%2 x&1 判断偶数奇数 性能对比
    推荐记录片系列:Ultimate Factories系列和MegaStructures系列
    计算机开放电子书汇总(包括二十多本python相关的图书教程)
    PyAutoGUI-python版的autoit/AHK
  • 原文地址:https://www.cnblogs.com/youxin/p/3921576.html
Copyright © 2011-2022 走看看