反射类的getStaticProperties返回继承的类属性


getStaticProperties of ReflectionClass returns inherited class properties

这是我的代码:

class A {
    public static $a = '1';
}
class B extends A {
    public static $b = '2';
}
$refclass = new ReflectionClass('B');
foreach ($refclass->getStaticProperties() as $key => $property)
    echo $key;

这段代码输出$a和$b。有没有办法在不获得继承的父类属性的情况下获得类属性?

foreach ($refclass->getStaticProperties() as $key => $property)
    if ($refclass->getProperty($key)->getDeclaringClass() == $refclass) {
        echo $key;
    }
}

或者,也许更优雅:

$props = array_filter($refclass->getProperties(ReflectionProperty::IS_STATIC), function ($prop) use ($refclass) {
    return $prop->getDeclaringClass() == $refclass;
});