Re: Private properties, Inheritance, Reflection (5.3)
| From: | Roman Borschel | Date: | Sat, 04 Apr 2009 11:08:26 +0000 |
| Subject: | Re: Private properties, Inheritance, Reflection (5.3) | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-43622@lists.php.net to get a copy of this message | ||
Hi,
Despite my almost non-existant C-skills I managed to put together a "patch" (or what I think is a patch) that does not break any tests.
My "patch" is simply to replace the first argument of zend_update_property/zend_read_property in ext/php_reflection.c like this:
within the getValue definition:
old: member_p = zend_read_property(Z_OBJCE_P(object), object, prop_name, strlen(prop_name), 1 TSRMLS_CC);
new: member_p = zend_read_property(ref->ce, object, prop_name, strlen(prop_name), 1 TSRMLS_CC);
within the setValue definition:
old: zend_update_property(Z_OBJCE_P(object), object, prop_name, strlen(prop_name), value TSRMLS_CC);
new: zend_update_property(ref->ce, object, prop_name, strlen(prop_name), value TSRMLS_CC);
So simply replacing Z_OBJCE_P(object) with ref->ce.
I assume "ce" stands for "class entry" and is a zend_class_entry. So instead of taking the zend_class_entry from the object, we take the one associated with the reflection property (at least thats what i assume it does).
I will now try to create a new test for this issue, taking of the existing tests as a template.
Any comments appreciated.
Roman
On Apr 4, 2009, at 9:02 AM, Roman Borschel wrote:
For what it matters, the same thing in Java returns the expected result: Class clazz = Foo.class; Field fooProp = clazz.getDeclaredField("foo"); fooProp.setAccessible(true); Bar bar = new Bar(); String value = (String)fooProp.get(bar); System.out.println(value); So is this a bug or some weird behavior I just don't understand yet? Roman On Apr 4, 2009, at 7:34 AM, Roman Borschel wrote:Hi, On Apr 4, 2009, at 1:53 AM, Johannes Schlüter wrote:-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.phpOn Fri, 2009-04-03 at 22:37 +0200, Roman Borschel wrote:Thanks for your answer. Yes, this works, but here $f is an instance of Foo, not Bar. When you make it an instance of Bar you get NULL. I think this is not correct, is it? Given that we have a ReflectionProperty of class Foo at hand, from my understanding it should look at an instance of Bar as a Foo and return the value. Am I missing something? Regards Roman -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.phpGiven 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 Foo { private $foo = "value"; } class Bar extends Foo {} $f = new Foo; // we have to know the context, as there might be multiple foo properties $r = new ReflectionProperty("Foo", "foo"); $r->setAccessible(true); var_dump($r->getValue($f)); works for me. johannes