PHP:多态抽象静态方法
我正在尝试做这样的事情,但我没有成功。
abstract class Animal
{
abstract static function getName();
static function sayName() { echo self::getName(); }
}
谢谢!
I'm trying to do something like this but i don't succeed.
abstract class Animal
{
abstract static function getName();
static function sayName() { echo self::getName(); }
}
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有两个问题:
You have two problems:
如果您能够提示如何您“没有成功”,那就太好了,但我想您在静态绑定中遇到了困难并且需要使用 PHP 5.3 中引入的最新静态绑定 。
It would have been nice if you'd have given a hint as to how you "don't succeed", but I suppose you're stumbling across static bindings and need to use late static bindings introduced in PHP 5.3.
我的猜测是您可能正在尝试实例化该类中的一个对象。
你不能。它是一个
抽象
类。将其子类化,然后实例化它。My guess is maybe you are trying to instantiate an object from that class.
You can't. It is an
abstract
class. Subclass it, and then instantiate that.那不会成功——你不能有一个抽象的静态函数。请参阅已接受的答案 为什么 PHP 5.2+ 不允许抽象静态类方法? 了解原因的详细信息。
That will not succeed- you cannot have an abstract static function. See the accepted answer Why does PHP 5.2+ disallow abstract static class methods? for details on why.