php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55495 Issues with numeric string keys in objects
Submitted: 2011-08-23 15:31 UTC Modified: 2011-08-24 08:45 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: roan dot kattouw at gmail dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: 5.4.0alpha3 OS: Ubuntu Natty
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: roan dot kattouw at gmail dot com
New email:
PHP Version: OS:

 

 [2011-08-23 15:31 UTC] roan dot kattouw at gmail dot com
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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-24 04:06 UTC] laruence@php.net
it's a known issue, there is only number key in array if it is ('looks like') a 
number.
 [2011-08-24 04:07 UTC] laruence@php.net
-Status: Open +Status: Wont fix
 [2011-08-24 08:45 UTC] roan dot kattouw at gmail dot com
First of all, this is about objects, not arrays. In arrays, the messed-up string-but-looks-like-a-number keys are still accessible, both through ["2"] and [2], but in objects they're completely inaccessible. The object member is there according to var_dump() but the only way to get its value is to cast the object to an array.

Second, I don't see how a "known issue" is "wontfix". That sounds like a direct contradiction to me.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Mar 05 21:01:24 2021 UTC