Private properties, Inheritance, Reflection (5.3)
| From: | Roman Borschel | Date: | Fri, 03 Apr 2009 20:37:45 +0000 |
| Subject: | Private properties, Inheritance, Reflection (5.3) | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-43618@lists.php.net to get a copy of this message | ||
Hi,
i've got a little question that involves private properties, inheritance and Reflection with PHP 5.3 RC1.
Given the following simple classes
class Foo {
private $foo = 'value';
public function getFoo() { return $this->foo; }
}
class Bar extends Foo {}
Obviously, given an instance of Bar, say $bar, $bar->getFoo() returns 'value'.
Now the question: How do I get at this value using reflection, given an instance of Bar?
$class = new \ReflectionClass('Foo');
$fooProp = $class->getProperty('foo');
$fooProp->setAccessible(true);
$bar = new Bar;
$value = $fooProp->getValue($bar);
var_dump($value); // NULL
var_dump($bar); // array(1) { ["�Foo�foo"]=> string(5) "value" }
How do I get at the value exposed by "var_dump($bar);" through reflection?
Obviously, I can't upcast like this: "$fooProp->getValue((Foo)$bar)".
I'm asking here because I have the bad feeling that this is might not (yet?) be possible.
Thanks!
Roman