PHP Conference Japan 2024

ReflectionClass::getReflectionConstants

(PHP 7 >= 7.1.0, PHP 8)

ReflectionClass::getReflectionConstants取得類別常數

說明

public ReflectionClass::getReflectionConstants(?int $filter = null): array

擷取反射的常數。

參數

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"
  }
}

另請參閱

新增筆記

使用者貢獻的筆記

此頁面沒有使用者貢獻的筆記。
To Top