ReflectionClass::getStaticProperties
(PHP 5, PHP 7, PHP 8)
ReflectionClass::getStaticProperties — staticプロパティを取得する
パラメータ
この関数にはパラメータはありません。
戻り値
staticプロパティを配列で返します。
変更履歴
バージョン | 説明 |
---|---|
8.3.0 |
ReflectionClass::getStaticProperties()
の戻り値の型が、?array
から array に変更されました。
|
参考
- ReflectionClass::getStaticPropertyValue() - staticプロパティの値を取得する
- ReflectionClass::setStaticPropertyValue() - staticプロパティの値を設定する
+add a note
User Contributed Notes 2 notes
jlennox @ google mail ¶
14 years ago
I had the need to recursive merge the results from a subclass with all of it's parents, and this was the resulting code:
<?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;
}
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.
joao dot felipe dot c dot b at gmail dot com ¶
8 years ago
getStaticProperties return a set of the property itself. It's diferente from getProperties(ReflectionProperty::IS_STATIC) because it return a set of ReflectionProperty class.
↑ and ↓ to navigate •
Enter to select •
Esc to close
Press Enter without
selection to search using Google