Reflection 在PHP中获取常量可见性

Reflection 在PHP中获取常量可见性,reflection,php-7.1,Reflection,Php 7.1,从PHP7.1开始,他们引入了常量可见性,我需要通过深入思考来阅读它。我甚至创建了我的ReflectionClass,如下所示: $rc = new ReflectionClass(static::class); 函数getConstants()返回名称/值映射,而getConstant($name)仅返回其值。两者都不会返回可见性信息。是否应该有一个类似于函数、属性等的ReflectionConst类 有没有其他方法可以获得这些信息?中讨论了对此的反射更改,尽管我不知道是否在其他地方记录了这

从PHP7.1开始,他们引入了常量可见性,我需要通过深入思考来阅读它。我甚至创建了我的
ReflectionClass
,如下所示:

$rc = new ReflectionClass(static::class);
函数
getConstants()
返回名称/值映射,而
getConstant($name)
仅返回其值。两者都不会返回可见性信息。是否应该有一个类似于函数、属性等的
ReflectionConst


有没有其他方法可以获得这些信息?

中讨论了对此的反射更改,尽管我不知道是否在其他地方记录了这些更改

新类是
ReflectionClassConstant
,包含相关方法(以及其他方法):

  • isPublic()
  • isPrivate()
  • isProtected()
ReflectionClass
有两种新方法:

  • getReflectionConstants()
    -返回ReflectionClassConstants的数组
  • getReflectionConstant()
    -按名称检索ReflectionClassConstant

例如:

class Foo
{
    private const BAR = 42;
}

$r = new ReflectionClass(Foo::class);

var_dump(
    $r->getReflectionConstants(),
    $r->getReflectionConstant('BAR')->isPrivate()
);
产出:

array(1) {
  [0]=>
  object(ReflectionClassConstant)#2 (2) {
    ["name"]=>
    string(3) "BAR"
    ["class"]=>
    string(3) "Foo"
  }
}
bool(true)

非常好用,谢谢!我希望它们没有被记录在任何地方这一事实会让我感到惊讶,但事实并非如此。