从 PHP 实例调用静态方法,将来弃用吗?

发布于 2024-11-16 00:11:59 字数 694 浏览 4 评论 0原文

虽然我知道在静态上下文中调用方法时 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油饼 2024-11-23 00:11:59

来自 Php 文档

声明为静态的属性不能通过实例化来访问
类对象(尽管静态方法可以)。

所以我认为它将得到长期的前瞻性支持。

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.

忘东忘西忘不掉你 2024-11-23 00:11:59

您始终可以使用:

$class = get_class($example);
$class::setData('some', 'data');

如果您想明确该方法是静态的。

或者,在类内部,您也可以在非静态方法中使用关键字 self 和 static (以及函数 get_called_class):

self::setData('some', 'data');

static::setData('some', 'data');

$class = get_called_class();
$class::setData('some', 'data');
  • self 引用声明该方法的类:如果方法位于 Animal 类中,Parrot 扩展了 Animal,即使在 Parrot 类型的对象中调用,self 也会引用 Animal 类。

  • static 引用正在使用的对象的类($this 的类),因此如果该方法位于 Animal 中并且在 Parrot 的实例上调用它,则它引用类 Parrot(就像get_class 和 get_called_class 函数)

,如果您在扩展 Animal 的对象 Parrot 中,并且以下代码是在 Animal 类中编写的:

self::setData('some', 'data');

is like

Animal::setData('some', 'data');

static::setData('some', 'data');

is like

Parrot::setData('some', 'data');

作为建议,我宁愿创建一个非静态方法,其名称信息丰富,可以调用静态方法,而不是从外部获取课程 时间。在我看来,它更加简洁、干净和清晰:

public function setStaticData($a,$b) {
    return static::setData($a,$b);
}

You can always use:

$class = get_class($example);
$class::setData('some', 'data');

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:

self::setData('some', 'data');

or

static::setData('some', 'data');

or

$class = get_called_class();
$class::setData('some', 'data');
  • 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:

self::setData('some', 'data');

is like

Animal::setData('some', 'data');

and

static::setData('some', 'data');

is like

Parrot::setData('some', 'data');

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:

public function setStaticData($a,$b) {
    return static::setData($a,$b);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文