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

参考

関連キーワード:  定数, 取得, クラス, ReflectionClass, ReflectionClassConstant, string, getReflectionConstants, Foo, filter, array