为什么PHP 5.2+不允许抽象静态类方法? [英] Why does PHP 5.2+ disallow abstract static class methods?

查看:61
本文介绍了为什么PHP 5.2+不允许抽象静态类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP 5.2中启用了严格警告之后,我看到了来自最初编写时没有严格警告的项目中的大量严格标准警告:

After enabling strict warnings in PHP 5.2, I saw a load of strict standards warnings from a project that was originally written without strict warnings:

严格标准:静态函数 Program :: getSelectSQL()不应是Program.class.inc

Strict Standards: Static function Program::getSelectSQL() should not be abstract in Program.class.inc

有问题的函数属于抽象父类Program,并且被声明为抽象静态,因为它应该在其子类(例如TVProgram)中实现.

The function in question belongs to an abstract parent class Program and is declared abstract static because it should be implemented in its child classes, such as TVProgram.

我确实在此处找到了该更改的引用:

I did find references to this change here:

删除了抽象静态类函数.由于疏忽大意,PHP 5.0.x和5.1.x允许在类中使用抽象静态函数.从PHP 5.2.x开始,只有接口可以拥有它们.

Dropped abstract static class functions. Due to an oversight, PHP 5.0.x and 5.1.x allowed abstract static functions in classes. As of PHP 5.2.x, only interfaces can have them.

我的问题是:有人可以清楚地解释为什么PHP中不应该有一个抽象的静态函数吗?

My question is: can someone explain in a clear way why there shouldn't be an abstract static function in PHP?

推荐答案

静态方法属于声明它们的类.在扩展类时,可以创建一个同名的静态方法,但实际上并没有实现静态抽象方法.

static methods belong to the class that declared them. When extending the class, you may create a static method of the same name, but you are not in fact implementing a static abstract method.

使用静态方法扩展任何类也是如此.如果扩展该类并创建具有相同签名的静态方法,则实际上并没有覆盖超类的静态方法

Same goes for extending any class with static methods. If you extend that class and create a static method of the same signature, you are not actually overriding the superclass's static method

EDIT (2009年9月16日)
对此进行更新.运行PHP 5.3,我看到抽象静态又回来了,不管是好是坏. (有关更多信息,请参见 http://php.net/lsb )

EDIT (Sept. 16th, 2009)
Update on this. Running PHP 5.3, I see abstract static is back, for good or ill. (see http://php.net/lsb for more info)

更正(由philfreo撰写)
在PHP 5.3中仍然不允许abstract static LSB 是相关的但有所不同.

CORRECTION (by philfreo)
abstract static is still not allowed in PHP 5.3, LSB is related but different.

这篇关于为什么PHP 5.2+不允许抽象静态类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆