从PHP中的实例调用静态方法,以后的弃用?

时间:2022-06-17 21:01:20

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:

虽然我理解在静态上下文中调用方法时$ 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');

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.

是否有计划弃用此类功能,或者我是否期望获得对此的支持?我使用error_reporting(-1)来确保一个非常严格的开发环境,并且还没有任何问题(PHP 5.3.6)但是我知道反向变得不受支持;也就是说,静态调用实例方法。

1 个解决方案

#1


29  

From the Php documentation:

从Php文档:

A property declared as static can not be accessed with an instantiated class object (though a static method can).

声明为static的属性无法使用实例化的类对象访问(尽管静态方法可以)。

So I think it will be forward-supported for a long time.

所以我认为它将在很长一段时间内得到前瞻性支持。

#1


29  

From the Php documentation:

从Php文档:

A property declared as static can not be accessed with an instantiated class object (though a static method can).

声明为static的属性无法使用实例化的类对象访问(尽管静态方法可以)。

So I think it will be forward-supported for a long time.

所以我认为它将在很长一段时间内得到前瞻性支持。