【发布时间】:2012-11-16 02:50:42
【问题描述】:
给定以下类结构:
class Foo {
protected static $_things = ['thing'];
}
class Bar extends Foo {
protected static $_things = [
'thing', 'other-thing'
];
}
class Baz extends Bar {
protected static $_things = [
'thing', 'other-thing', 'something-else'
];
}
class Quux extends Baz {
// Note the lack of "other-thing"
protected static $_things = [
'thing', 'something-else', 'one-more'
];
}
重构并保持数组元素更干燥的最佳方法是什么?例如,“thing”元素只能定义一次(Foo),“other-thing”元素只能定义一次(Bar),以此类推。
在实践中,这个数组非常大,有时可以有多达 4 或 5 级继承,每个都需要以某种特殊的方式“修改”这个数组,无论是添加 还是删除 元素。
我一直在玩弄可以进行适当修改的初始化方法的想法,但想先看看是否有更好的方法。
【问题讨论】:
标签: php design-patterns inheritance