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

From: Date: Wed, 16 Aug 2006 20:50:09 +0000
Subject: #36693 [Com]: get_object_vars does not return private members
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-100874@lists.php.net to get a copy of this message
ID: 36693 Comment by: thomas at epiphantastic dot com Reported By: iain at iaindooley dot com Status: No Feedback Bug Type: Class/Object related Operating System: FreeBSD 6.0 PHP Version: 5.1.2 New Comment: Forgot to say, I'm running PHP 5.1.2 Previous Comments: ------------------------------------------------------------------------ [2006-08-16 20:48:52] 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-03-23 01:00:05] 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-03-15 16:04:48] 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-11 03:21:18] 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); } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=36693&edit=1

« previous php.bugs (#100874) next »