|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-05-15 14:41 UTC] bruno dot caillaud at cndp dot fr
Description: ------------ I read the Bug #42814 which is supposed to be closed, however, I have reproduced this bug in the last version 5.2.6 this is a sample code : <?php class Base { private $basePrivateProperty; protected $baseprotectedProperty; function getProperties () { return get_object_vars ( $this ); } } class Child extends Base { private $childPrivateProperty; protected $childprotectedProperty; } echo "Base :<br/>\n"; $father = new Base(); var_dump($father->getProperties()); echo "<br/>Child :<br/>\n"; $children = new Child(); var_dump($children->getProperties()); ?> Running this sample code Result on different versions of php 5: PHP version 5.2.0 to 5.2.3 : Base : array(2) { ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } Child : array(3) { ["childPrivateProperty"]=> NULL ["childprotectedProperty"]=> NULL ["baseprotectedProperty"]=> NULL } PHP version 5.2.4 to 5.2.6: Base : array(2) { ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } Child : array(3) { ["childprotectedProperty"]=> NULL ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } I think the good behaviour is the one returned by versions 5.2.0 to 5.2.3. in 5.2.4 version and up, the child can access to his parent's private property but not to his own private property Reproduce code: --------------- <?php class Base { private $basePrivateProperty; protected $baseprotectedProperty; function getProperties () { return get_object_vars ( $this ); } } class Child extends Base { private $childPrivateProperty; protected $childprotectedProperty; } echo "Base :<br/>\n"; $father = new Base(); var_dump($father->getProperties()); echo "<br/>Child :<br/>\n"; $children = new Child(); var_dump($children->getProperties()); ?> Expected result: ---------------- Base : array(2) { ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } Child : array(3) { ["childPrivateProperty"]=> NULL ["childprotectedProperty"]=> NULL ["baseprotectedProperty"]=> NULL } Actual result: -------------- Base : array(2) { ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } Child : array(3) { ["childprotectedProperty"]=> NULL ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 14:00:01 2025 UTC |
After reading once more the documentation for get_object_vars () and its comments, I understand that this method returns all access-authorized properties of the object you pass to it. So when you implement get_object_vars in a object method like get_object_vars($this), get_object_vars can access all propreties of the object (in the definition of a class, you can access all properties : private, protected and public). That's why in version less than 5.2.4, Child class can access to his own private properties. But now in version 5.2.4 and up, the object own private properties are not available but its parent's private properties are available! This is "the bug"... When I do Child->getProperties(), the "$this" (of get_object_vars($this)) is a "Child" object, not a "Base" object even if the getProperties method is implemented in Base Class. In version 5.2.0 to 5.2.3, all works well. I found an other similar bug with property_exists() function in all subversion of 5.2: this this the code : class Base { private $basePrivateProperty; protected $baseprotectedProperty; function getProperties () { echo "className = ".get_class($this)." :<br/> "; return get_object_vars ( $this ); } function isPropertyExists($propName) { echo " - className = ".get_class($this)." => "; return property_exists($this, $propName); } } class Child extends Base { private $childPrivateProperty; protected $childprotectedProperty; } this is the result (here in version 5.2.6) : className = Base : array(2) { ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } father->isPropertyExists("childPrivateProperty") - className = Base => bool(false) father->isPropertyExists("basePrivateProperty") - className = Base => bool(true) className = Child : array(3) { ["childprotectedProperty"]=> NULL ["basePrivateProperty"]=> NULL ["baseprotectedProperty"]=> NULL } children->isPropertyExists("childPrivateProperty") - className = Child => bool(false) children->isPropertyExists("basePrivateProperty") - className = Child => bool(true) As you can see, with the children object (class Child), It's not working...