Overriding static members in derived classes in PHP

前端 未结 3 1234
Happy的楠姐
Happy的楠姐 2021-01-11 17:44


        
3条回答
  •  情深已故
    2021-01-11 18:05

    You have to re-implment base class method; try with:

    class Derived extends Base {
      protected static $c = 'derived';
    
      public static function getC() {
        return self::$c;
      }
    }
    

    As you see, this solution is very useless, because force to re-write all subclassed methods.

    The value of self::$c depends only on the class where the method was actually implemented, not the class from which it was called.

提交回复
热议问题