Get the defining class for a constant in PHP

前端 未结 1 1794
我在风中等你
我在风中等你 2021-01-25 06:37

I am wanting to use reflection to obtain a list of the constants defined by a class in PHP.

Currently using reflection I can get a list of the constants, but this also i

相关标签:
1条回答
  • 2021-01-25 07:34

    By default, PHP has no function I'm aware of that would already remove the parent classes and interfaces constants. So you would need to do that your own:

    $reflection = new ReflectionClass('Bar');
    $buffer = $reflection->getConstants();
    foreach (
        array($reflection->getParentClass())
        + $reflection->getInterfaces()
        as $fill
    ) {
        $buffer = array_diff_key($buffer, $fill->getConstants());
    }
    

    The result in $buffer is the array you are looking for:

    Array
    (
        [CHILD_CONST] => child
        [BAR_CONST] => bar_const
    )
    
    0 讨论(0)
提交回复
热议问题