#36693 [NEW]: get_object_vars does not return private members

From: Date: Sat, 11 Mar 2006 02:21:19 +0000
Subject: #36693 [NEW]: get_object_vars does not return private members
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-94169@lists.php.net to get a copy of this message
From:             iain at iaindooley dot com
Operating system: FreeBSD 6.0
PHP version:      5.1.2
PHP Bug Type:     Class/Object related
Bug description:  get_object_vars does not return private members

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);
}

-- 
Edit bug report at http://bugs.php.net/?id=36693&edit=1
-- 
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=36693&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=36693&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=36693&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=36693&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=36693&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=36693&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=36693&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=36693&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=36693&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=36693&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=36693&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=36693&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=36693&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=36693&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=36693&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=36693&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=36693&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=36693&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=36693&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=36693&r=mysqlcfg


Thread (7 messages)

« previous php.bugs (#94169) next »