doumao1047 2012-10-22 00:13
浏览 32
已采纳

这是正常的吗?

I don't get how this late static binding works.

abstract class A{

  final public static function doprint(){
    print get_called_class() . '<br>';
  }


  public static function wrapper(){
    self::doprint();
    static::doprint();
  }

}

class A2 extends A{}    

A2::wrapper();

get_called_class() prints A2 in both cases, even when I called the doprint method with self. why?

  • 写回答

2条回答 默认 最新

  • dongzhang1987 2012-10-22 00:29
    关注

    get_called_class() always returns the class you actually call. You call A2:: so it's A2.

    On my site there's a tutorial with an LSB singleton abstract class. I'm not linking here because there's always a zombie vigilante that comes and removes the links without even looking. But it's in my description.

    The catch with LSB is that a method in A can call a method in B which can call a method in A back. See this example:

    header('Content-Type: text/plain'); // Pretty text output
    // LSB Parent
    class A {
        // NOLSB: Will call Current Class method
        static public function TriggerSelf() {
            self::OverrideMe();
        }
        // LSB: Will call Inheriting Class method (or Current if none Inherits)
        static public function TriggerStatic() {
            static::OverrideMe();
        }
        // Method to Override
        static public function OverrideMe() {
            echo 'A here', PHP_EOL;
        }
    }
    
    // LSB Child
    class B extends A {
        // Override by LSB
        static public function OverrideMe() {
            echo 'B here', PHP_EOL;
        }
    }
    
    A::TriggerSelf(); // <- NO LSB
    A::TriggerStatic(); // <- LSB (but not inheritance)
    
    B::TriggerSelf(); // <- NO LSB
    B::TriggerStatic(); // <- LSB (with inheritance, so it works)
    

    See how B::TriggerStatic() allows A to call a B method while B::TriggerSelf() calls a A method. That's LSB. Parent class static methods can call child class static methods. It's pretty much static abstracts :)

    Study the example, it'll make sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?