Given the following class structure:
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'
];
}
What would be the best way to refactor this and keep the array elements more DRY? For example, the “thing” element should only be defined once (in Foo), the “other-thing” element should only be define once (in Bar), and so on.
In practice, this array is very large, and there can sometimes be up to 4 or 5 levels of inheritance, each needing to “modify” this array in some special way, whether that’s adding or removing elements.
I’ve been toying with the idea of initialization methods that would do the appropriate modifications, but wanted to see if there were any better ways first.
Simplest solution I can think of (heavily based on the singleton pattern).
I don’t think there is any compile-time way to do what you are looking for.