php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46812 get_class_vars() does not include visible private variable looking at subclass
Submitted: 2008-12-09 10:13 UTC Modified: 2010-02-16 23:48 UTC
From: phpbug dot classvars at sub dot noloop dot net Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.*, 6CVS (2009-04-30) OS: *
Private report: No CVE-ID: None
 [2008-12-09 10:13 UTC] phpbug dot classvars at sub dot noloop dot net
Description:
------------
Even after bug #45862, #46761 and #46795 there is something really weird going on with get_class_vars(). It seems to be the consensus of the developers that get_class_vars() should return all properties of the given class that are _visible_ from the context calling get_class_vars() (nevermind that the docs claim "returns ... public properties of the class" (see #46795)). (Also, #31543 seems to contradict everything else) 

But get_class_vars() does not return visible private properties when invoked on a subclass. 

In the attached code, the second call to dumpClass should return 'private_a', as $private_a would still be visible in methods in A, even if the object in question actually is of type B.

As a side note, I find it a bit strange that the behaviour of get_class_vars() function changed between 5.2.6 and 5.2.7 (it broke a real-world inhouse app here, for example) :)


Reproduce code:
---------------
<?php
  class A {
    private $private_a;
    public static function dumpClass($class) {
      print_r(get_class_vars($class));
    }
  }
  class B extends A {
    private $private_b;
  }

  A::dumpClass('A');
  A::dumpClass('B');


Expected result:
----------------
Array
(
    [private_a] => 
)
Array
(
    [private_a] => 
)


Actual result:
--------------
Array
(
    [private_a] => 
)
Array
(
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-12 14:33 UTC] msaraujo@php.net
ZEND_FUNCTION(get_class_vars)
{
	char *class_name;
	int class_name_len;
	zend_class_entry **pce;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &class_name, &class_name_len) == FAILURE) {
		return;
	}

	if (zend_lookup_class(class_name, class_name_len, &pce TSRMLS_CC) == FAILURE) {
		RETURN_FALSE;
	} else {
		array_init(return_value);
		zend_update_class_constants(*pce TSRMLS_CC);
	   
	   if ((*pce)->parent) {

		add_class_vars((*pce)->parent, &(*pce)->parent->default_properties, return_value TSRMLS_CC);
		add_class_vars((*pce)->parent, CE_STATIC_MEMBERS((*pce)->parent), return_value TSRMLS_CC)
	   }
	   else {
		add_class_vars(*pce, &(*pce)->default_properties, return_value TSRMLS_CC);
		add_class_vars(*pce, CE_STATIC_MEMBERS(*pce), return_value TSRMLS_CC);
	   }
		
	}
}
 [2009-04-30 14:06 UTC] phpbug dot classvars at sub dot noloop dot net
This problem still occurs with http://snaps.php.net/php5.2-200904301230.tar.bz2:

% ~/devel/php/php-5.2-200904301230/bin/php -v
PHP 5.2.10-dev (cli) (built: Apr 30 2009 16:00:22) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

% ~/devel/php/php-5.2-200904301230/bin/php test.php   
---------------
Array
(
    [private_a] => 
)
Array
(
)
 [2009-05-22 13:21 UTC] lbarnaud@php.net
To be documented:

- Returns an associative array of default public properties of the class.
+ Returns an associative array of declared properties visible from the current scope, with their default value.
 [2010-02-16 23:45 UTC] svn@php.net
Automatic comment from SVN on behalf of joey
Revision: http://svn.php.net/viewvc/?view=revision&revision=295188
Log: Fix PHP Bug #46812, applying patch suggested by lbarnaud@php.net
 [2010-02-16 23:48 UTC] joey@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC