#47808 [Com]: hasProperty inconsistent with getProperty and new ReflectionProperty

From: Date: Tue, 12 May 2009 16:41:49 +0000
Subject: #47808 [Com]: hasProperty inconsistent with getProperty and new ReflectionProperty
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-137211@lists.php.net to get a copy of this message
ID: 47808 Comment by: voyager at voyd dot net Reported By: sven dot arduwie at gmail dot com Status: Open Bug Type: Reflection related Operating System: * PHP Version: 5.3.0RC1 New Comment: I am also experiencing this issue. It appears to only be a problem with extended classes. For example: class A { private $var; } class B extends A { } $ro = new ReflectionObject(new A()); echo $ro->hasProperty('var') ? 'true' : 'false', "\n"; // returns true echo $ro->getProperty('var'), "\n"; // prints property $ro = new ReflectionObject(new B()); echo $ro->hasProperty('var') ? 'true' : 'false', "\n"; // returns true echo $ro->getProperty('var'), "\n"; // throws exception Previous Comments: ------------------------------------------------------------------------ [2009-05-06 00:33:51] kalle@php.net You must check the visibility of a property aswell from the ReflectionProperty instance created by getProperty(): if(($property = $reflector->getProperty($property)) && $property->isPublic()) { /* callable */ } However it looks trival, I'll leave this for one of the maintainers ------------------------------------------------------------------------ [2009-03-27 14:59:20] sven dot arduwie at gmail dot com Description: ------------ In the reproduce code hasProperty() in Base::__get() returns true while getProperty() throws an exception with message "Fatal error: Uncaught exception 'ReflectionException' with message 'Property test does not exist'" A more appropriate message would be "Fatal error: Uncaught exception 'ReflectionException' with message 'Cannot access non-public member Child::test'", OR, and perhaps this would be best, change the behavior of hasProperty() to return false. The current behavior is really annoying if you're, like me, trying to write a __get() method that returns the value of private/protected properties using 'getters', e.g.: getMyProperty() for property $myProperty. Reproduce code: --------------- <?php class Base { public function __get($property) { $reflector = new ReflectionObject($this); if ($reflector->hasProperty($property)) { return $reflector->getProperty($property)->getValue(); } } } class Child extends Base { private $test = 'This is a test.'; } class Test extends Child { } $test = new Test; var_dump($test->test); Expected result: ---------------- getProperty() to throw "Fatal error: Uncaught exception 'ReflectionException' with message 'Cannot access non-public member Child::test'" or hasProperty() to return false Actual result: -------------- hasProperty() returns true while getProperty() throws a message with an inappropriate message ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=47808&edit=1

« previous php.bugs (#137211) next »