考慮下面的類結構:可繼承數組的設計模式?
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'
];
}
什麼是重構這一點,並保持數組元素越乾的最好方法?例如,「東西」元素應該只定義一次(在Foo
),「其他東西」元素應該只定義一次(在Bar
)等等。
實際上,這個數組非常大,有時候可能會有多達4或5個級別的繼承,每個需要以某種特殊方式「修改」這個數組,無論是添加還是刪除元素。
我一直在玩的初始化方法的想法,將做適當的修改,但想看看是否有任何更好的方法第一。
感謝您的建議。這實際上與我一直在玩的東西非常相似,只是我在構造函數中進行了合併而不是getter。 – FtDRbwLXw6