I have a following class hierarchy, which shown in a reproduction script below:
<?php
header('Content-Type: text/plain');
class A
{
public $config = array(
'param1' => 1,
'param2' => 2
);
public function __construct(array $config = null){
$this->config = (object)(empty($config) ? $this->config : array_merge($this->config, $config));
}
}
class B extends A
{
public $config = array(
'param3' => 1
);
public function __construct(array $config = null){
parent::__construct($config);
// other actions
}
}
$test = new B();
var_dump($test);
?>
Output:
object(B)#1 (1) {
["config"]=>
object(stdClass)#2 (1) {
["param3"]=>
int(1)
}
}
What I wanted, is that A::$config
not be overriden by B::$config
. There might be a lot of descendant classes from B
, where I would like to change $config
, but I need that those $config
values to merge / overwrite if match $config
values of all it's parents.
Q: How can I do that ?
I've tried to use array_merge()
but in non-static mode those variables just override themselves. Is there a way to achieve merge effect for class tree without static
(late static binding) ?