在PHP中覆盖派生类中的静态成员

pow*_*boy 12 php inheritance static

<?php
class Base {
  protected static $c = 'base';

  public static function getC() {
    return self::$c;
  }
}

class Derived extends Base {
  protected static $c = 'derived';
}

echo Base::getC(); // output "base"
echo Derived::getC();    // output "base", but I need "derived" here!
?>
Run Code Online (Sandbox Code Playgroud)

那么什么是最好的解决方法?

dec*_*eze 9

解决此问题的最佳方法是升级到PHP 5.3,后期静态绑定可用.如果这不是一个选项,你将不得不重新设计你的课程.

  • @powerboy它是**向后**兼容,意味着为5.2编写的脚本将在5.3上运行,最大的部分.一些小事可能已经改变.见这里:http://www.php.net/manual/en/migration53.php (3认同)