我有下面的类层次结构,显示在下面的复制脚本中:
<?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);
?>
输出:
object(B)#1 (1) {
["config"]=>
object(stdClass)#2 (1) {
["param3"]=>
int(1)
}
}
我想要的是A::$config
不能被B::$config
所取代。B
中可能有很多子类,我想在其中更改$config
,但如果这些$config
值与它所有父级的$config
值匹配,我需要合并/覆盖这些值。
问:我该怎么做?
我尝试过使用array_merge()
,但在非静态模式下,这些变量只是覆盖它们自己。有没有一种方法可以在没有static
(后期静态绑定)的情况下实现类树的合并效果?
与其用要在构造函数中更改的值来声明$config
属性,不如将这些值声明为默认值。Orangepill的回答中也描述了这一点:
class A
{
public $config;
private $defaults = array(
'param1' => 1,
'param2' => 2,
);
public function __construct(array $config = array())
{
$this->config = (object)($config + $this->defaults);
}
}
那里有些曲折;通过将$config
构造函数参数的默认值声明为空数组,您可以像上面那样使用数组运算符来简化代码。$config
中未定义的密钥由$this->defaults
填充。
扩展类看起来非常相似:
class B extends A
{
private $defaults = array(
'param3' => 1
);
public function __construct(array $config = array())
{
parent::__construct($config + $this->defaults);
}
}
您可以重组扩展类实例化的方式
class B extends A
{
private $defaults = array('param3' => 1);
public function __construct(array $config = null){
parent::__construct($config?array_merge($this->defaults, $config):$this->defaults);
}
}
您可以使用ReflectionClass
来完成此操作。从内省$this
开始,使用getProperty()
,然后使用getParentClass()
对父类及其父类等执行同样的操作,并将生成的数组合并在一起。
不过,这可能不是您所面临问题的最佳解决方案。
我相信您正在寻找以下内容。是否改编自继承子类中的静态属性而不重新声明?
<?php
class MyParent {
public static $config = array('a' => 1, 'b' => 2);
public static function getConfig() {
$ret = array();
$c = get_called_class();
do {
$ret = array_merge($c::$config, $ret);
} while(($c = get_parent_class($c)) !== false);
return $ret;
}
}
class MyChild extends MyParent {
public static $config = array('a' => 5, 'c' => 3, 'd' => 4);
public function myMethod($config) {
$config = array_merge(self::getConfig(), $config);
}
}
class SubChild extends MyChild {
public static $config = array('e' => 7);
}
var_export(MyChild::getConfig());
// result: array ( 'a' => 5, 'b' => 2, 'c' => 3, 'd' => 4, )
$mc = new MyChild();
var_export($mc->myMethod(array('b' => 6)));
// result: array ( 'a' => 5, 'b' => 6, 'c' => 3, 'd' => 4, )
var_export(SubChild::getConfig());
// result: array ( 'a' => 5, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 7, )