I often use properties in my classes that store an array of options. I'd like to be able to somehow merge those options from defaults declared in a parent class.
I demonstrated with some code.
class A
{
public $options = array('display'=>false,'name'=>'John');
}
class B extends A
{
public $options = array('name'=>'Mathew');
}
Now when I create B
, then I'd like $options
to contain a merged array from A::options
What happens now is this.
$b = new B();
print_r($b);
array('name'=>'Mathew');
I would like something like this using array_merge_recursive()
.
array('display'=>false,'name'=>'Mathew');
- Maybe it's something I could do in the constructor?
- Is it possible to make this a behavior of
class A
? So that I don't always have to implement the same code in all subclasses. - Could I use reflection to auto find array properties in both classes and merge them?