PHP中继承抽象类的静态类
我偶然发现了一个有趣的问题/错误,我最终解决了它,但解决方案不是我所期望或希望的。
设置很简单。有一个抽象静态类Factory,它具有对单例(Registry)的引用以及两个静态属性,model和 >表。
有许多静态类基于此抽象类构建/继承自该抽象类,它们都有一个 init() 方法,在该方法中设置模型和表< /strong> 属性。当然,由于 model 和 table 是静态的,因此它们只能具有一个值,该值对于抽象 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目标是在不同的静态子类中为
$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.