虽然我知道在静态上下文中调用方法时$this
变量不可用,但为了帮助将应用程序组件与另一个组件解耦,我认为从实例中调用静态方法是有意义的。例如:
class MyExample{
private static $_data = array();
public static function setData($key, $value){
self::$_data[$key] = $value;
}
// other non-static methods, using self::$_data
}
// to decouple, another class or something has been passed an instance of MyExample
// rather than calling MyExample::setData() explicitly
// however, this data is now accessible by other instances
$example->setData('some', 'data');
是否有计划弃用这种功能,或者我期望这种功能的支持是正确的吗?我使用error_reporting(-1)
来确保一个非常严格的开发环境,到目前为止(PHP5.3.6)还没有任何问题,但是我意识到相反的情况变得不受支持;也就是说,实例方法被静态调用。
发布于 2011-11-03 00:30:33
https://stackoverflow.com/questions/6398658
复制相似问题