|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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); } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 04:00:01 2025 UTC |
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.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" }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").