Can I get the value of a private property with Reflection?

Please, note that accepted answer will not work if you need to get the value of a private property which comes from a parent class.

For this you can rely on getParentClass method of Reflection API.

Also, this is already solved in this micro-library.

More details in this blog post.


In case you need it without reflection:

public function propertyReader(): Closure
{
    return function &($object, $property) {
        $value = &Closure::bind(function &() use ($property) {
            return $this->$property;
        }, $object, $object)->__invoke();
         return $value;
    };
}

and then just use it (in the same class) like this:

$object = new SomeObject();
$reader = $this->propertyReader();
$result = &$reader($object, 'some_property');

class A
{
    private $b = 'c';
}

$obj = new A();

$r = new ReflectionObject($obj);
$p = $r->getProperty('b');
$p->setAccessible(true); // <--- you set the property to public before you read the value

var_dump($p->getValue($obj));

getProperty throws an exception, not an error. The significance is, you can handle it, and save yourself an if:

$ref = new ReflectionObject($obj);
$propName = "myProperty";
try {
  $prop = $ref->getProperty($propName);
} catch (ReflectionException $ex) {
  echo "property $propName does not exist";
  //or echo the exception message: echo $ex->getMessage();
}

To get all private properties, use $ref->getProperties(ReflectionProperty::IS_PRIVATE);

Tags:

Php

Reflection