note 97791 added to reflectionclass.getstaticproperties
| From: | jlennox at googlemail | Date: | Sat, 08 May 2010 17:03:02 +0000 |
| Subject: | note 97791 added to reflectionclass.getstaticproperties | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169073@lists.php.net to get a copy of this message | ||
I had the need to recursive merge the results from a subclass with all of it's parents, and
this was the resulting code:
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;
}
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.
----
Server IP: 69.147.83.197
Probable Submitter: 71.248.164.32
----
Manual Page -- http://www.php.net/manual/en/reflectionclass.getstaticproperties.php
Edit -- https://master.php.net/note/edit/97791
Del: integrated -- https://master.php.net/note/delete/97791/integrated
Del: useless -- https://master.php.net/note/delete/97791/useless
Del: bad code -- https://master.php.net/note/delete/97791/bad+code
Del: spam -- https://master.php.net/note/delete/97791/spam
Del: non-english -- https://master.php.net/note/delete/97791/non-english
Del: in docs -- https://master.php.net/note/delete/97791/in+docs
Del: other reasons-- https://master.php.net/note/delete/97791
Reject -- https://master.php.net/note/reject/97791
Search -- https://master.php.net/manage/user-notes.php