PHP: Hypertext Preprocessor (original) (raw)

`I had the need to recursive merge the results from a subclass with all of it's parents, and this was the resulting code:

currentClass=currentClass = currentClass=class; $joinedProperties = array(); do { reflection=newReflectionClass(reflection = new ReflectionClass(reflection=newReflectionClass(class); staticProperties=staticProperties = staticProperties=reflection->getStaticProperties(); foreach ($staticProperties as name=>name => name=>value) { if (is_array($value)) { if (isset($joinedProperties[$name])) joinedProperties[joinedProperties[joinedProperties[name] = array_merge($value, joinedProperties[joinedProperties[joinedProperties[name]); else joinedProperties[joinedProperties[joinedProperties[name] = $value; } else { if (isset($joinedProperties[$name])) joinedProperties[joinedProperties[joinedProperties[name][] = $value; else joinedProperties[joinedProperties[joinedProperties[name] = array($value); } } } while ($class = get_parent_class($class)); return $joinedProperties; }Using this function: class base { public static $Test = array("foo1", "foo2"); } class sub extends base { public static $Test = "sub"; }print_r(GetStaticPropertiesRecursive("sub")); ?>

That outputs:
Array
(
[Test] => Array
(
[0] => foo1
[1] => foo2
[2] => sub
)

)

The merge follows the rules of array_merge on duplicate keys.

`