vendredi 15 janvier 2016

PHP: Fatal error when trying to access private superclass property through reflection

What I am trying to do is to extend a third-party library by subclassing one of it's classes. Unfortunately, I need access to a private field, so I am trying to use refelction to do so:

class LibraryClass {
    private $property;

    public function method() { ... }
}

class MyClass extends LibraryClass {

    public function method() {
        parent::method();

        $reflectionClass = new ReflectionClass('LibraryClass');
        $property = $reflectionClass->getProperty('property');
        $property->setAccessible(true);
        $value = $property->getValue($this); // <--- Fatal error here.
    }
}

What happens is that I am getting a '500 Internal error' and nothing in PHP's error log. I guess that the problem is due to polymorhic use of the reflection function (I pass an instance of MyClass as LibraryClass), but shouldn't this work, or at least throw a meaningful exception?

Any ideas on what could be the problem or how to achieve this differently? Thank you.





Aucun commentaire:

Enregistrer un commentaire