|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47808 hasProperty inconsistent with getProperty and new ReflectionProperty
Submitted: 2009-03-27 14:59 UTC Modified: 2015-03-18 18:29 UTC
Avg. Score:4.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: sven dot arduwie at gmail dot com Assigned: requinix (profile)
Status: Closed Package: Reflection related
PHP Version: 5.*, 6CVS (2009-05-14) OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
50 - 44 = ?
Subscribe to this entry?

 [2009-03-27 14:59 UTC] sven dot arduwie at gmail dot com
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:
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;

Expected result:
getProperty() to throw "Fatal error: Uncaught exception 'ReflectionException' with message 'Cannot access non-public member Child::test'"


hasProperty() to return false

Actual result:
hasProperty() returns true while getProperty() throws a message with an inappropriate message


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-06 00:33 UTC]
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-05-12 16:41 UTC] voyager at voyd dot net
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
 [2009-08-04 15:04 UTC]
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

 [2015-03-18 12:41 UTC] f1690403 at trbvm dot com
6 years later, the problem still exists in PHP Version 5.3.10-1ubuntu3.15
 [2015-03-18 18:29 UTC]
-Assigned To: +Assigned To: requinix
 [2015-03-18 18:29 UTC]

Both sven.arduwie's and voyager's code work correctly as of 5.3.2. Can you be more descriptive than just "problem still exists"? And have you tested with PHP versions that are still supported?
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 09:03:35 2021 UTC