(PHP 7 >= 7.1.0, PHP 8)
ReflectionClass::getReflectionConstants — 取得類別常數
filter
可選的篩選器,用於篩選所需的常數可見性。它使用 ReflectionClassConstant 常數 進行設定,預設為所有常數可見性。
一個 ReflectionClassConstant 物件的陣列。
版本 | 說明 |
---|---|
8.0.0 |
新增了 filter 參數。 |
範例 #1 基本的 ReflectionClass::getReflectionConstants() 範例
<?php
class Foo {
public const FOO = 1;
protected const BAR = 2;
private const BAZ = 3;
}
$foo = new Foo();
$reflect = new ReflectionClass($foo);
$consts = $reflect->getReflectionConstants();
foreach ($consts as $const) {
print $const->getName() . "\n";
}
var_dump($consts);
?>
上述範例將輸出類似以下的內容
FOO BAR BAZ array(3) { [0]=> object(ReflectionClassConstant)#3 (2) { ["name"]=> string(3) "FOO" ["class"]=> string(3) "Foo" } [1]=> object(ReflectionClassConstant)#4 (2) { ["name"]=> string(3) "BAR" ["class"]=> string(3) "Foo" } [2]=> object(ReflectionClassConstant)#5 (2) { ["name"]=> string(3) "BAZ" ["class"]=> string(3) "Foo" } }