简体   繁体   English

从PHP抽象类继承的静态类

[英]Static classes inheriting from abstract class in PHP

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 . 有一个抽象的静态类Factory ,它引用了一个单例( Registry )以及两个静态属性modeltable

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. 有许多基于该抽象类/从其继承的静态类,并且它们都具有init()方法,在其中可以设置模型属性。 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. 当然,由于模型是静态的,因此它们只能具有一个值,该值对于抽象Factory类的所有子类都是相同的。 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. 这似乎有点麻烦(并且不是很干),但是在我看来,这是唯一的解决方案,如果我想让(1)从抽象Factory类继承的类和(2)保持静态。

Is my assumption correct or is there another approach that I am missing? 我的假设是正确的还是我缺少另一种方法?

The goal is to have different values for $model and $table in different static child classes? 目标是在不同的静态子类中为$model$table提供不同的值?

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. 我认为这是一种好习惯-通过将功能保留在所属位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM