【问题标题】:Dynamic binding in static method php静态方法php中的动态绑定
【发布时间】:2012-09-04 15:24:10
【问题描述】:
class A
{
  static function get_name_derived_class()
  {
      //This function must return the name of the real class
      //Is it possible without insert a methon in B class?
  {
}

class B extends A
{

}

B::test()

我想在基类中有一个静态方法,它返回真实(派生)类的名称,而不在其中插入特定方法。可能吗? 谢谢

【问题讨论】:

    标签: php static-methods dynamic-binding


    【解决方案1】:
    <?php
    
    class A
    {
        static function test()
        {
            return get_called_class();
        }
    }
    
    class B extends A
    {
    }
    
    echo B::test();
    

    需要 PHP >= 5.3.0。请参阅Late Static Bindings上的 PHP 手册

    【讨论】:

      猜你喜欢
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 2021-11-29
      • 2011-01-07
      相关资源
      最近更新 更多