从PHP中的实例调用静态方法,将来弃用吗? [英] Call static method from instance in PHP, future deprecation?
问题描述
虽然我知道在静态上下文中调用方法时$this
变量不可用,以帮助将我的应用程序组件彼此分离,但我认为从实例中调用静态方法是有意义的.例如:
While I understand the $this
variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For example:
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)
一起使用以确保非常严格的开发环境,并且到目前为止还没有任何问题( PHP 5.3.6 ),但是我知道相反的情况不再得到支持;也就是说,实例方法被静态调用.
Are there plans to deprecate this sort of functionality, or am I right to expect support for this going forward? I work with error_reporting(-1)
to ensure a very strict development environment, and there aren't any issues as of yet (PHP 5.3.6) however I am aware of the reverse becoming unsupported; that is, instance methods being called statically.
推荐答案
来自 Php文档:
不能通过实例化访问声明为静态的属性 类对象(尽管静态方法可以).
A property declared as static can not be accessed with an instantiated class object (though a static method can).
所以我认为它将得到长期支持.
So I think it will be forward-supported for a long time.
这篇关于从PHP中的实例调用静态方法,将来弃用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!