PHP中继承抽象类的静态类

发布于 2024-12-09 08:16:26 字数 693 浏览 0 评论 0原文

我偶然发现了一个有趣的问题/错误,我最终解决了它,但解决方案不是我所期望或希望的。

设置很简单。有一个抽象静态类Factory,它具有对单例(Registry)的引用以及两个静态属性,model >表

有许多静态类基于此抽象类构建/继承自该抽象类,它们都有一个 init() 方法,在该方法中设置模型表< /strong> 属性。当然,由于 modeltable 是静态的,因此它们只能具有一个值,该值对于抽象 Factory 类的所有子类都是相同的。这就是问题/错误。

然而,我的目标是让每个子类都有自己的模型,所以我被迫声明模型表< /strong> 在每个子类中作为静态属性。这看起来有点麻烦(而且不是很干),但在我看来,如果我想要(1)类继承抽象 Factory 类并且(2),这是唯一的解决方案保持静止。

我的假设正确还是我缺少另一种方法?

I have stumbled upon an interesting problem/bug that I eventually solved, but the solution is not what I expected or would like it to be.

The setup is simple. There is an abstract static class Factory, which has a reference to a singleton (Registry) as well as two static properties, model and table.

There are a number of static classes building upon/inheriting from this abstract class and they all have an init() method in which they set the model and table properties. Of course, since model and table are static they can only have one value which is the same for all the child classes of the abstract Factory class. This is the problem/bug.

However, my aim is to have each child class have its own model and table so I am forced to declare model and table in each child class as a static property. This seems a bit cumbersome (and not very DRY), but it seems to me that this is the only solution if I want to have (1) the classes inherit from the abstract Factory class and (2) remain static.

Is my assumption correct or is there another approach that I am missing?

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

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

发布评论

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

评论(1

香草可樂 2024-12-16 08:16:26

目标是在不同的静态子类中为 $model$table 提供不同的值?

假设是这种情况,我不知道如果不在每个子类中显式定义不同的它们,您将如何实现这一点。

我不认为这很麻烦。我认为这是一个很好的做法——将功能保留在其所属的位置。

The goal is to have different values for $model and $table in different static child classes?

Assuming that is the case, I don't see how You could possibly accomplish that without explicitly defining them different in each child class.

I don't think it is cumbersome. I see it as a good practice - by keeping functionality where it belongs.

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