使用后期静态绑定访问父级


Accessing parent with late static binding

我希望能够获得后期静态绑定类的父级。我写了一个不起作用的代码示例来澄清我的意思:

class A
{
    public static function getCombinedProperties()
    {
        $a = static::$properties; // Late static binding means this will work
        if(isset(static::parent::$properties))  // Calling static::parent obviously does not work
            $a = array_merge_recursive($a, static::parent::$properties);
        return $a;
    }
}
class B extends A
{
    public static $properties = ['fruits' => ['pineapple', 'mango']];
}
class C extends B
{
    public static $properties = ['fruits' => ['apple', 'banana'], 'vegetables' => ['carrot', 'pea']];
}
B::getCombinedProperties(); //desired output ['fruits' => ['pineapple', 'mango']]
C::getCombinedProperties(); //desired output ['fruits' => ['apple', 'banana', 'pineapple', 'mango'], 'vegetables' => ['carrot', 'pea']]

当从类C调用方法时,类A是否可以访问类B的$properties属性?

这似乎有效,尽管感觉很粗糙:

class A
{
    public static function getCombinedProperties()
    {
        $a = static::$properties; // Late static binding means this will work
        $class = new 'ReflectionClass(get_called_class());
        $parent = $class->getParentClass();
        $parentName = $parent->getName();
        if(isset($parentName::$properties))
            $a = array_merge_recursive($a, $parentName::$properties);
        return $a;
    }
}