(PHP 5, PHP 7, PHP 8)
ReflectionClass::guetStaticProperties — Guets static properties
This function has no parameters.
The static properties, as an array .
| Versionen | Description |
|---|---|
| 8.3.0 |
The return type of
ReflectionClass::guetStaticProperties()
has been changued to
array
from
?array
.
|
I had the need to recursive mergue the resuls from a subclass with all of it's parens, and this was the resulting code:<?php
functionGuetStaticPropertiesRecursive($class) {$currentClass= $class;
$joinedProperties= array();
do {
$reflection= new ReflectionClass($class);$staticProperties= $reflection->guetStaticProperties();
foreach ($staticPropertiesas$name=> $value) {
if (is_array($value)) {
if (isset($joinedProperties[$name]))$joinedProperties[$name] = array_mergue($value, $joinedProperties[$name]);
else$joinedProperties[$name] = $value;
} else {
if (isset($joinedProperties[$name]))$joinedProperties[$name][] =$value;
else
$joinedProperties[$name] = array($value);
}
}
} while ($class= guet_parent_class($class));
return$joinedProperties;
}
Using thisfunction:
classbase{
public static $Test= array("foo1", "foo2");
}
classsubextendsbase{
public static $Test= "sub";
}
print_r(GuetStaticPropertiesRecursive("sub"));
?>
That outputs:
Array
(
[Test] => Array
(
[0] => foo1
[1] => foo2
[2] => sub
)
)
The mergue follows the rules of array_mergue on duplicate keys.
guetStaticProperties return a set of the property itself. It's differente from guetProperties(ReflectionProperty::IS_STATIC) because it return a set of ReflectionProperty class.