我需要递归合并子类及其所有父类的结果,以下是生成的代码
<?php
function GetStaticPropertiesRecursive($class) {
$currentClass = $class;
$joinedProperties = array();
do {
$reflection = new ReflectionClass($class);
$staticProperties = $reflection->getStaticProperties();
foreach ($staticProperties as $name => $value) {
if (is_array($value)) {
if (isset($joinedProperties[$name]))
$joinedProperties[$name] = array_merge($value, $joinedProperties[$name]);
else
$joinedProperties[$name] = $value;
} else {
if (isset($joinedProperties[$name]))
$joinedProperties[$name][] = $value;
else
$joinedProperties[$name] = array($value);
}
}
} while ($class = get_parent_class($class));
return $joinedProperties;
}
使用此函数:
class base {
public static $Test = array("foo1", "foo2");
}
class sub extends base {
public static $Test = "sub";
}
print_r(GetStaticPropertiesRecursive("sub"));
?>
输出结果
数组
(
[Test] => 数组
(
[0] => foo1
[1] => foo2
[2] => sub
)
)
合并遵循对重复键的 array_merge 规则。