From: Operating system: Ubuntu Natty PHP version: 5.4.0alpha3 Package: Class/Object related Bug Type: Bug Bug description:Issues with numeric string keys in objects
Description: ------------ If you have an object with numeric string keys such as "1", serialize() and unserialize() will not round-trip cleanly: if $foo = array("1" =>"foo"), unserialize(serialize($foo)) will be array(1=>"foo"). I'm not actually sure this is a bug in serialize()/unserialize(); it seems to be a bug in json_decode() instead. Somewhat counter-intuitively, arrays or objects with numeric string keys are really hard to produce. Constructs like array("1"=>"foo") or even $s = "1"; $arr[$s] = "foo"; produce an array with int(1) as the key rather than string("1"). The same is true for objects. The only thing that produces numeric string keys, AFAIK, is json_decode(). A very related bug, and the reason I noticed this in the first place, is that if you have an object with numeric keys and set $s = "2" , $obj->{$s} will not return the value for key 2. Worse, it seems there's no way to access the value short of casting the object to an array and using $arr[2]. Related bugs have been filed and closed, but they don't seem to have addressed this issue properly: #48557: patch applied to 5.2 and 5.3; I found this bug in 5.4 #48171: basically the same problem, marked as bogus referring to #48959 , which is about arrays not objects. It claims to have been documented in the manual, but again that refers to arrays not objects Test script: --------------- <?php error_reporting(E_ALL); $s = "2"; $i = 2; $foo = json_decode('{"1":"foo","2":"bar"}'); $ser = serialize($foo); $bar = unserialize($ser); var_dump($foo); var_dump($ser); var_dump($bar); var_dump($foo->{$s}); var_dump($foo->{$i}); var_dump($bar->{$s}); var_dump($bar->{$i}); Expected result: ---------------- object(stdClass)#1 (2) { ["1"]=> string(3) "foo" ["2"]=> string(3) "bar" } string(55) "O:8:"stdClass":2:{s:1:"1";s:3:"foo";s:1:"2";s:3:"bar";}" object(stdClass)#2 (2) { ["1"]=> string(3) "foo" ["2"]=> string(3) "bar" } string(3) "bar" string(3) "bar" string(3) "bar" string(3) "bar" Actual result: -------------- object(stdClass)#1 (2) { ["1"]=> string(3) "foo" ["2"]=> string(3) "bar" } string(55) "O:8:"stdClass":2:{s:1:"1";s:3:"foo";s:1:"2";s:3:"bar";}" object(stdClass)#2 (2) { [1]=> string(3) "foo" [2]=> string(3) "bar" } string(3) "bar" string(3) "bar" Notice: Undefined property: stdClass::$2 in /home/catrope/php-5.4.0alpha3/testcase.php on line 13 NULL Notice: Undefined property: stdClass::$2 in /home/catrope/php-5.4.0alpha3/testcase.php on line 14 NULL -- Edit bug report at https://bugs.php.net/bug.php?id=55495&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=55495&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=55495&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=55495&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=55495&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=55495&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=55495&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=55495&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=55495&r=needscript Try newer version: https://bugs.php.net/fix.php?id=55495&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=55495&r=support Expected behavior: https://bugs.php.net/fix.php?id=55495&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=55495&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=55495&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=55495&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=55495&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=55495&r=dst IIS Stability: https://bugs.php.net/fix.php?id=55495&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=55495&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=55495&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=55495&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=55495&r=mysqlcfg