PHP 任何组合/继承解决方案,以避免多重继承?
我试图以最有效的方式解决设计难题,但我倾向于最终认为我真的需要在这里多重继承。所以,我在这里向高手请教。我正在创建一个活动记录库,它将使用不同的 name
和 value
变量执行几乎相同的操作。我使用的是 PHP 5.2.13
假设我有这些类:
class Property {
protected $_props = array();
function set($name, $value) {
$this->_props[$name] = $value;
};
}
class ColorProperties extends Property {
function red($value) {
$this->set('red', $value);
}
function blue($value) {
$this->set('blue', $value);
}
}
class LayoutProperties extends Property {
function x($value) {
$this->set('x', $value);
}
function y($value) {
$this->set('y', $value);
}
}
现在我需要创建一个类 ShapeProperties
,它将继承自 ColorProperties
和 LayoutProperties.所以:
$shape = new ShapeProperties();
$shape->x(10);
$shape->red(255);
我需要这种方式的原因是在 IDE 中自动完成,并为每个属性提供巨大的 PHPDoc 注释块。所以,似乎我出于绝望而倾向于复制/粘贴。
旧身体(编辑前)
我已经有了这些类(块):
class Red {
function red();
}
class LightRed {
function light_red();
}
class Blue {
function blue();
}
class LightBlue {
function light_blue();
}
class Green {
function green();
}
class LightGreen {
function light_green();
}
现在我需要使用这些块构建大量的类,即:
class RGB extends Red, Green, Blue {
function red();
function blue();
function green();
}
class RLRB extends Red, LightRed, Blue {
function red();
function light_red();
function blue();
}
所以如果我用接口切换单词类,我会得到我需要的,但我需要工作实现而不需要加载样板代码。有没有办法在 PHP 中解决这个问题?
I'm trying to solve a design puzzle in the most efficient way, but I tend to end thinking that I really need a multiple inheritance here. So, here I am, asking the pros. I'm creating an active-record lib that will perform almost identical actions with different name
and value
variables. I'm on PHP 5.2.13
Suppose I have these classes:
class Property {
protected $_props = array();
function set($name, $value) {
$this->_props[$name] = $value;
};
}
class ColorProperties extends Property {
function red($value) {
$this->set('red', $value);
}
function blue($value) {
$this->set('blue', $value);
}
}
class LayoutProperties extends Property {
function x($value) {
$this->set('x', $value);
}
function y($value) {
$this->set('y', $value);
}
}
Now I need to create a class ShapeProperties
that will inherit from both, ColorProperties
and LayoutProperties
. So that:
$shape = new ShapeProperties();
$shape->x(10);
$shape->red(255);
The reason why I need it that way, is to have auto-completion in IDE with huge PHPDoc comment block for each property. So, seems that I'm leaning towards copy/paste out of despair.
OLD BODY (BEFORE EDITS)
I've got these classes (blocks):
class Red {
function red();
}
class LightRed {
function light_red();
}
class Blue {
function blue();
}
class LightBlue {
function light_blue();
}
class Green {
function green();
}
class LightGreen {
function light_green();
}
And now I need to build a numerous amount of classes using these blocks, ie:
class RGB extends Red, Green, Blue {
function red();
function blue();
function green();
}
class RLRB extends Red, LightRed, Blue {
function red();
function light_red();
function blue();
}
So if I switch word class with interface, I'll get what I need, but I need working implementation without loads of boilerplate code. Is there a way/approach to work this around in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用魔术函数和委托来执行类似的操作。我希望你的真实案例与颜色无关,因为避免使用一些单行函数有点矫枉过正。
您可以提供任意的实现列表并循环浏览它们,直到找到具有适当方法名称的实现。使用所有参数调用它。可能会出现方法冲突,就像多重继承中发生的情况一样,只不过这里没有编译器会告诉您。您还会失去参数验证等。
You can use magic functions and delegation to do something similar. I hope your real case is not really about colors, because this would be a bit overkill to avoid a few one line functions.
You can provide an arbitrary list of implementations and cycle through them until you find one that has the appropriate method name. Call it with all of the arguments. There can be method clashes, just like it happens with multiple inheritance, except that here, no compiler will tell you. You also lose the argument validation and such.
在不久的将来,php 5.4 新功能 -
traits
将成为可能。这看起来像这样:但就目前而言,最好的方法似乎是使用静态方法和依赖注入,如下所示:
但您仍然必须声明所有函数并使其从其他类调用方法。
In the near future it will be possible with php 5.4 new feature -
traits
. This will look like this:But for now, the best method seems to be using static methods and dependency injections, like this:
but you still have to declare all functions and make it call method from other class.