ID: 46600
Updated by: [EMAIL PROTECTED]
Reported By: Matt at mpcm dot com
Status: Verified
Bug Type: JSON related
Operating System: *
PHP Version: 5CVS, 6CVS (2008-11-18)
New Comment:
I believe this is not a bug, but a feature.
An object *can't* have an empty property (while an array can).
If you want to use json_decode() with json containing empty key, either
access those empty key using special keyword "_empty_", or put the
optionnal $assoc parameter of json_decode() to true to get result as an
array.
If you want objects to support empty keys, I believe this is not going
to happen soon, as this is enforced by a specific error message.
Fatal error: Cannot access empty property in php shell code on line 1
Please note that a property name starting with NULL character won't
work either.
Previous Comments:
------------------------------------------------------------------------
[2008-11-18 17:35:30] matt at mpcm dot com
The language seems to create a key that cannot be reached, so even if
this `bug` is fixed, we am still facing a broader issue it seems.
<?
$key = "";
$o = (object) array($key=>4,"example"=>8);
var_dump($o);
print 'blank key test:' . (isset($o->$key)?'true':'false');
print $o->{$key};
var_dump($o->$key);
?>
output:
object(stdClass)#1 (2) {
[""]=>
int(4)
["example"]=>
int(8)
}
blank key test:false<br />
<b>Fatal error</b>: Cannot access empty property in
<b>PHPDocument1</b> on line <b>8</b><br />
All throws Notice: line 4 - Illegal member variable name
------------------------------------------------------------------------
[2008-11-18 15:43:05] matt at mpcm dot com
All the work arounds I am looking at are throwing Error Text: Illegal
member variable name when I convert/cast an object with a blank
property.
Is this json_decode `bug` a result of a larger object mechanism
limitation inside of php's object handling?
------------------------------------------------------------------------
[2008-11-18 03:13:51] Matt at mpcm dot com
Description:
------------
json_decode() treats empty property name as "_empty_" not "". This was
fixed in #41504 for arrays, but not for objects. (seems to happen in PHP
Version 5.2.4-2ubuntu5.3 and 5.3.0alpha2.
Reproduce code:
---------------
<?
$s = '{"":"test"}';
var_dump(json_decode($s));
?>
Expected result:
----------------
object(stdClass)#1 (1) { [""]=> string(4) "test" }
Actual result:
--------------
object(stdClass)#2 (1) { ["_empty_"]=> string(4) "test" }
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46600&edit=1