php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45008 get_object_vars() PPP issue
Submitted: 2008-05-15 14:41 UTC Modified: 2009-07-09 20:36 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: bruno dot caillaud at cndp dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.*, 6CVS (2009-04-25) OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bruno dot caillaud at cndp dot fr
New email:
PHP Version: OS:

 

 [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 }

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-16 21:30 UTC] jani@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-16 21:43 UTC] jani@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-19 11:50 UTC] 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...
 [2009-07-09 20:36 UTC] jani@php.net
See bug #30659
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC