PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #36693 get_object_vars does not return private members
Submitted:11 Mar 2006 3:21am UTC Modified: 16 Aug 2006 9:13pm UTC
From:iain at iaindooley dot com Assigned to:
Status:No Feedback Category:Class/Object related
Version:5.1.2 OS:FreeBSD 6.0
Votes:7 Avg. Score:3.4 ± 1.7 Reproduced:0 of 1 (0.0%)
View/Vote Add Comment Developer Edit Submission

Have you experienced this issue?
Rate the importance of this bug to you:

[11 Mar 2006 3:21am 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);
}
[15 Mar 2006 4:04pm 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.
[23 Mar 2006 1:00am 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".
[16 Aug 2006 8:48pm 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.
[16 Aug 2006 8:50pm UTC] thomas at epiphantastic dot com
Forgot to say, I'm running PHP 5.1.2
[16 Aug 2006 9:13pm UTC] tony2001@php.net
That's correct behavior, since you're calling it outside of the object
scope.

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC