|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45862 get_class_vars is inconsistent with 'protected' and 'private' variables
Submitted: 2008-08-19 10:27 UTC Modified: 2008-08-22 01:10 UTC
From: ilewis at uk dot ibm dot com Assigned: felipe (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.2CVS, 5.3CVS, 6CVS OS: fedora 8
Private report: No CVE-ID: None
 [2008-08-19 10:27 UTC] ilewis at uk dot ibm dot com
get_class_vars() sometimes returns protected and private variables, depending on the calling scope. However, the behaviour is inconsistent with the visibility of the variables. Private variables are found when calling from child scope (they aren't visible), and protected variables are _not_ found from parent scope (they are visible).

It feels as though get_class_vars() should respect visibility entirely or not at all

The expected output below is what would be produced if get_class_vars respected visibility consistently. I've put a patch (built against snap php5.3-200808180830) that produces this behaviour on pastebin:

I used the old logic from property_exists() for the patch (although that has now been changed so that property_exists no longer respects visibility)

Reproduce code:
class Ancestor {
  function test() {

class Tester extends Ancestor {
  static protected $prot = "protected var";
  static private $priv = "private var";

class Child extends Tester {
  function test() { var_dump(get_class_vars("Tester")); }

echo "\n From parent scope\n";
$parent = new Ancestor();
echo "\n From child scope\n";
$child = new Child();

Expected result:
 From parent scope
array(1) {
  string(13) "protected var"
string(13) "protected var"

 From child scope
array(1) {
  string(13) "protected var"

Actual result:
 From parent scope
array(0) {
string(13) "protected var"

 From child scope
array(2) {
  string(13) "protected var"
  string(11) "private var"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-22 01:10 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Hi Ilewis, I've modified a bit your patch (because it did fail in some cases) and commited in 5.2, 5.3 and HEAD, and also added some tests.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 01:01:31 2024 UTC