php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36693 get_object_vars does not return private members
Submitted: 2006-03-11 03:21 UTC Modified: 2006-08-16 21:13 UTC
Votes:9
Avg. Score:3.8 ± 1.6
Reproduced:2 of 3 (66.7%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: iain at iaindooley dot com Assigned:
Status: No Feedback Package: Class/Object related
PHP Version: 5.1.2 OS: FreeBSD 6.0
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
32 + 23 = ?
Subscribe to this entry?

 
 [2006-03-11 03:21 UTC] iain at iaindooley dot com
Description:
------------
a call to get_class_vars from within the class includes private members, but get_object_vars does not

this bug report:

http://bugs.php.net/bug.php?id=27798&edit=2

says that this was fixed about a year ago.

Reproduce code:
---------------
<?
class SomeClass implements Serializable
{
    private $member;
    public $another;

    function SomeClass()
    {
        $this->member = 'member value';
        $this->another = 'another value';
    }

    public function serialize()
    {
        print_r(get_class_vars(__CLASS__));
        print_r(get_object_vars($this));
    }

    public function unserialize($serialized)
    {
    }
}

class AnotherClass extends SomeClass
{
    function AnotherClass()
    {
        $this->SomeClass();
    }
}

$obj = new AnotherClass();
serialize($obj);
?>


Expected result:
----------------
i would expect to see:

Array
(
    [member] =>
    [another] =>
)
Array
(
    [member] => member value
    [another] => another value
)


Actual result:
--------------
Array
(
    [member] =>
    [another] =>
)
Array
(
    [another] => another value
)

i have been able to work around this problem by doing:
public function serialize()
{
    $serialized = array();

    foreach(array_keys(get_class_vars(__CLASS__)) as $key)
        eval('$serialized[\''.$key.'\'] =        
                                       $this->'.$key.';');
    $this->serializeMore($serialized);
    return serialize($serialized);
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-15 16:04 UTC] mike@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.1-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.1-win32-latest.zip

Cannot reproduce.
 [2006-03-23 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-08-16 20:48 UTC] thomas at epiphantastic dot com
I'm experiencing this same problem, running PHP on Windows with both Apache and IIS. Here's the code:

<?php
class TestClass {
	private $var1 = "private";
	protected $var2 = "protected";
	public $var3 = "public";
}

$o = new TestClass();
foreach(array_keys(get_object_vars($o)) as $key)
	echo $key;
?>

I'd expect to see 3 values outputted but only one comes back.
 [2006-08-16 20:50 UTC] thomas at epiphantastic dot com
Forgot to say, I'm running PHP 5.1.2
 [2006-08-16 21:13 UTC] tony2001@php.net
That's correct behavior, since you're calling it outside of the object scope.
 [2010-02-22 15:04 UTC] nacho at ziggo dot nl
Description:
------------
This seems to be broken again on version 5.3.1 (Debian/Apache)

Reproduce code:
---------------
<?php
class Foo {

	private $a = 'a';

	public $b = 'b';

	protected $c = 'c';

	public function __construct() {}

	public function Export() {

		$vars = get_object_vars($this);

		return $vars;

	}

}

class Bar extends Foo {

	private $d = 'd';

	public $e = 'e';

	protected $f = 'f';

}

$foo = new Foo();
$bar = new Bar();

var_dump($foo->Export());
var_dump($bar->Export());
?>

Expected result:
----------------
array(3) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(1) "b"
  ["c"]=>
  string(1) "c"
}
array(5) {
  ["d"]=>
  string(1) "d"
  ["e"]=>
  string(1) "e"
  ["f"]=>
  string(1) "f"
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(1) "b"
  ["c"]=>
  string(1) "c"
}

Actual result:
--------------
array(3) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(1) "b"
  ["c"]=>
  string(1) "c"
}
array(5) {
  ["d"]=>
  string(1) "d"
  ["e"]=>
  string(1) "e"
  ["f"]=>
  string(1) "f"
  ["b"]=>
  string(1) "b"
  ["c"]=>
  string(1) "c"
}
 [2010-02-22 15:17 UTC] nacho at ziggo dot nl
Correction (I was actually testing on another machine where 5.1 is running). Actual output on 5.3.1 is:

array(3) {
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(1) "b"
  ["c"]=>
  string(1) "c"
}
array(5) {
  ["e"]=>
  string(1) "e"
  ["f"]=>
  string(1) "f"
  ["a"]=>
  string(1) "a"
  ["b"]=>
  string(1) "b"
  ["c"]=>
  string(1) "c"
}

While "a" should indeed not have to be visible to "Bar", as it is a private property of "Foo", like the output of version 5.1 show, on 5.3.1 it seems as it is interchanged with the private property "d" of "Bar", which should be visible to itself ("Bar").
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 30 03:01:30 2024 UTC