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

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

问题描述

在 PHP 5.2 中启用严格警告后,我看到了一个最初没有严格警告的项目的严格标准警告:

<块引用>

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

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

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

<块引用>

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

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

解决方案

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

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

编辑(2009 年 9 月 16 日)
对此进行更新.运行 PHP 5.3,我看到抽象静态又回来了,无论好坏.(有关详细信息,请参阅 http://php.net/lsb)

更正(作者:philfreo)
abstract static 在 PHP 5.3 中仍然不被允许,LSB 相关但不同.>

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:

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

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:

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.

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 (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)

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

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

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