为什么ReflectionClass在可访问时不会读取其父项的私有属性?

时间:2014-03-11 15:21:16

标签: php reflection

我有一个抽象类和一个实现类:

abstract class AbstractClass {
    public function __construct() { }

    public function get($property) {
        $rc = new \ReflectionClass($this);
        $p = $rc->getProperty($property);
        $p->setAccessible(true);
        return $p->getValue($this);
    }

    public function set($property, $value) {
        $rc = new \ReflectionClass($this);
        $p = $rc->getProperty($property);
        $p->setAccessible(true);
        $p->setValue($this, $value);
        return $this;
    }
}

abstract class MetadataClass extends AbstractClass {
    private $metadataA;
    private $metadataB;
}

class ActualClass extends MetadataClass {
    private $realField1;
    private $realField2;
}

我声明并尝试从/向子类获取/设置属性:

$ac = new ActualClass();
$ac->set('metadataA', 'Hello World');
echo $ac->get('metadataA');

我收到了:

ReflectionException 0 Property metadataA does not exist in AbstractClass.php on line 13

这是ReflectionClass的限制还是我错过了什么?这是still a bug in PHP吗?

0 个答案:

没有答案