php.net |  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
Description:
------------
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:
http://pastebin.com/m10403f42

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:
---------------
<?php
class Ancestor {
  function test() {
    var_dump(get_class_vars("Tester"));
    var_dump(Tester::$prot);
  }
}

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();
$parent->test();
echo "\n From child scope\n";
$child = new Child();
$child->test();
?>


Expected result:
----------------
 From parent scope
array(1) {
  ["prot"]=>
  string(13) "protected var"
}
string(13) "protected var"

 From child scope
array(1) {
  ["prot"]=>
  string(13) "protected var"
}


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

 From child scope
array(2) {
  ["prot"]=>
  string(13) "protected var"
  ["priv"]=>
  string(11) "private var"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-08-22 01:10 UTC] felipe@php.net
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
http://snaps.php.net/.
 
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.

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 17:01:30 2024 UTC