ID:               45008
 Updated by:       j...@php.net
-Summary:          $this is not what you expect..
 Reported By:      bruno dot caillaud at cndp dot fr
-Status:           Assigned
+Status:           Verified
 Bug Type:         Scripting Engine problem
 Operating System: *
-PHP Version:      5.2.6
+PHP Version:      5.*, 6CVS (2009-04-25)
 Assigned To:      helly


Previous Comments:
------------------------------------------------------------------------

[2008-05-19 11:50:59] bruno dot caillaud at cndp dot fr

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...

------------------------------------------------------------------------

[2008-05-16 21:43:04] j...@php.net

And what's the bug here anyway? As it clearly says in the manual page
for get_object_vars(): "Gets the public properties of the given
object".
Private is not public and as the method is in context of the "Base"
class, why would it include private properties of extending class?
(Disclaimer: This is how _I_ understand this :)

You get expected result if you define getProperties() method in the
Child class tool..

------------------------------------------------------------------------

[2008-05-16 21:30:29] j...@php.net

Just a note about bug #42814: "No feedback" is not same as "Closed" in
which case there would be an entry in the NEWS file about a fixed bug..

------------------------------------------------------------------------

[2008-05-15 14:41:48] 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 }


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=45008&edit=1

Reply via email to