在PHP中覆盖派生类中的静态成员 [英] Overriding static members in derived classes in PHP

查看:124
本文介绍了在PHP中覆盖派生类中的静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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!
?>

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

So what's the best workaround?

推荐答案

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

The best way to solve this is to upgrade to PHP 5.3, where late static bindings are available. If that's not an option, you'll unfortunately have to redesign your class.

这篇关于在PHP中覆盖派生类中的静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆