PHP 严格标准:这段代码有什么问题?

发布于 2024-12-20 00:39:20 字数 837 浏览 6 评论 0原文

可能的重复:
为什么 PHP 5.2+ 不允许抽象静态类方法?< /a>
为什么不能从抽象中调用抽象函数PHP 中的类?

我在 PHP 5.3.8 上运行此代码:

abstract class Geometry
{
    abstract public static function fromArray(array $array);
}

class Point extends Geometry
{
    public static function fromArray(array $point)
    {
        return new self($point[0], $point[1]);
    }
}

并收到以下错误:

严格标准:静态函数 Geometry::fromArray() 不应该是抽象的

  • 这种方法有什么问题吗?
  • 有没有可行的替代方案来强制具体类实现此工厂方法?

Possible Duplicate:
Why does PHP 5.2+ disallow abstract static class methods?
Why can't you call abstract functions from abstract classes in PHP?

I'm running this code on PHP 5.3.8:

abstract class Geometry
{
    abstract public static function fromArray(array $array);
}

class Point extends Geometry
{
    public static function fromArray(array $point)
    {
        return new self($point[0], $point[1]);
    }
}

And receive the following error:

Strict Standards: Static function Geometry::fromArray() should not be abstract

  • What's wrong with this approach?
  • Any viable alternative to force concrete classes to implement this factory method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

孤蝉 2024-12-27 00:39:20

您能否将 Geometry 设为一个接口,然后让 Point 实现它?

Could you make Geometry an interface, and have Point implement it?

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