php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69238 getProperties() Doesn't list undeclared class properties
Submitted: 2015-03-14 23:44 UTC Modified: 2015-03-15 11:30 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: geolim4 at gmail dot com Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 5.6.6 OS: Debian 3.2.65-1+deb7u1 x86_64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: geolim4 at gmail dot com
New email:
PHP Version: OS:

 

 [2015-03-14 23:44 UTC] geolim4 at gmail dot com
Description:
------------
It seem that ReflectionClass::getProperties() doesn't fetch undeclared properties of an object, even if you specify all the filters such ReflectionProperty::IS_xxxxxx


Test script:
---------------
class foo
{
	public $bar = false;
	private $foobar = false;

	function __construct()
	{
		$this->bar = false;
		$this->foobar = true;
		$this->baz = true;
	}
}

$data = new foo();

$class = new \ReflectionClass($data);
$properties = $class->getProperties();

\var_dump( $properties );

exit;


Expected result:
----------------
array(2) {
  [0]=>
  &object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "bar"
    ["class"]=>
    string(3) "foo"
  }
  [1]=>
  &object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(6) "foobar"
    ["class"]=>
    string(3) "foo"
  }
  [2]=>
  &object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(6) "baz"
    ["class"]=>
    string(3) "foo"
  }
}


Actual result:
--------------
array(2) {
  [0]=>
  &object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(3) "bar"
    ["class"]=>
    string(3) "foo"
  }
  [1]=>
  &object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(6) "foobar"
    ["class"]=>
    string(3) "foo"
  }
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-14 23:46 UTC] geolim4 at gmail dot com
As you can see the test was done inside a namespace, the bug is also reproduced in global scope.
 [2015-03-15 02:06 UTC] laruence@php.net
I think it's not a bug but a expected behavior, the baz is a dynamic property of a instance of class Foo
 [2015-03-15 02:14 UTC] geolim4 at gmail dot com
In that case the method name has a nonsense, the method should be named getDeclaredProperties() instead. Or an insufficiently completed docbook.

But in that case, why the value returned is the dynamic value, and not declared value? I don't get it.

If it's an excepted behavior, it must be explicitly mentioned in the docbook.
 [2015-03-15 02:14 UTC] reeze@php.net
-Status: Open +Status: Not a bug
 [2015-03-15 02:14 UTC] reeze@php.net
You need use ReflectionObject instead of ReflectionClass, since dynamic properties is not part of Class
 [2015-03-15 02:25 UTC] geolim4 at gmail dot com
Indeed, i missed it. But now i realize that getProperties() still have a bug, why does when i loop on $properties and i check call getValue() on each property, this method returns the DYNAMIC value and not the declared value. As you said that's Class related and not object related.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jan 02 12:01:29 2025 UTC