note 97781 deleted from reflectionclass.getproperties by danbrown
| From: | danbrown@php.net | Date: | Fri, 07 May 2010 14:57:46 +0000 |
| Subject: | note 97781 deleted from reflectionclass.getproperties by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169061@lists.php.net to get a copy of this message | ||
Note Submitter: nicolassauvaget at yahoo dot fr
----
I would like to add some details about the script of muratyaman at gmail dot com.
He uses a recursive function in order to navigate through the parent classes to get their
attributes.
I would mention it is not necessary as the following script shows the behaviour of
reflection::GetProperties() with a class hierarchy :
1 <?php
2
3 class God
4 {
5 public $publicGodAttr = NULL;
6 protected $protectedGodAttr = NULL;
7 private $ProtectedGodAttr = NULL;
8 }
9
10 class Base extends God
11 {
12 public $publicBaseAttr = NULL;
13 protected $protectedBaseAttr = NULL;
14 private $ProtectedBaseAttr = NULL;
15 }
16
17
18 class Foo extends Base
19 {
20 public $alpha = 1;
21 protected $beta = 2;
22 private $gamma = 3;
23
24 public function listProperties ()
25 {
26 $reflect = new ReflectionObject ($this);
27 //foreach ($reflect->getProperties (ReflectionProperty::IS_PUBLIC +
28 // ReflectionProperty::IS_PROTECTED) as $prop)
29 foreach ($reflect->getProperties () as $prop)
30 {
31 print $prop->getName ()."\n";
32 }
33 }
34 }
35
36 $foo = new Foo ();
37 $foo->listProperties ();
38
39 ?>
Here is the output :
alpha
beta
gamma
publicBaseAttr
protectedBaseAttr
publicGodAttr
protectedGodAttr
As you can see, GetProperties() respects the visibilty and the class hierarchy. There is no need to
be recursive (and check the parent classes).