从 PHP 实例调用静态方法,将来弃用吗?
虽然我知道在静态上下文中调用方法时 $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)
来确保非常严格的开发环境,并且到目前为止还没有任何问题(PHP 5.3.6),但我知道反向变得不受支持;也就是说,实例方法被静态调用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 Php 文档:
所以我认为它将得到长期的前瞻性支持。
From the Php documentation:
So I think it will be forward-supported for a long time.
您始终可以使用:
如果您想明确该方法是静态的。
或者,在类内部,您也可以在非静态方法中使用关键字 self 和 static (以及函数 get_called_class):
或
self
引用声明该方法的类:如果方法位于 Animal 类中,Parrot 扩展了 Animal,即使在 Parrot 类型的对象中调用,self 也会引用 Animal 类。static
引用正在使用的对象的类($this 的类),因此如果该方法位于 Animal 中并且在 Parrot 的实例上调用它,则它引用类 Parrot(就像get_class 和 get_called_class 函数),如果您在扩展 Animal 的对象 Parrot 中,并且以下代码是在 Animal 类中编写的:
is like
和
is like
作为建议,我宁愿创建一个非静态方法,其名称信息丰富,可以调用静态方法,而不是从外部获取课程 时间。在我看来,它更加简洁、干净和清晰:
You can always use:
If you want to be explicit about the method being static.
Or, from inside the class you can use the keywords self and static (and the function get_called_class) also in non static methods:
or
or
self
references the class where the method is declared: if the method is in class Animal and Parrot extends Animal, self will reference class Animal even if called in an object of type Parrot.static
references the class of the object being used (the class of $this), so if the method is in Animal and it is called on an instance of Parrot it references class Parrot (like the get_class and get_called_class functions)So, if you are in object Parrot that extends Animal, and the following code is written in class Animal:
is like
and
is like
As a suggestion I would rather make a non static method with an informative name that calls the static one, rather then get the class from outside every time. It seems to me more concise, clean and clear: