Call static method from instance in PHP, future deprecation?

泄露秘密 提交于 2019-11-30 01:08:51

问题


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');

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.


回答1:


From the Php documentation:

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.



来源:https://stackoverflow.com/questions/6398658/call-static-method-from-instance-in-php-future-deprecation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!