Edit report at https://bugs.php.net/bug.php?id=46600&edit=1
ID: 46600
Comment by: matt at mpcm dot com
Reported by: Matt at mpcm dot com
Summary: "_empty_" key in objects (see #41504)
Status: No Feedback
Type: Bug
Package: JSON related
Operating System: *
PHP Version: 5CVS, 6CVS (2008-11-18)
Block user comment: N
Private report: N
New Comment:
To illustrate the current collision state, as this came up on the JSMentors list recently.
<?php
$a =
'{"_empty_":"a","":"b","":"c"}';
echo "as object: " . json_encode(json_decode($a));
echo "\n";
echo "as array: " . json_encode(json_decode($a, true));
PHP 5.4.17 (cli) (built: Aug 25 2013 02:03:38)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
Previous Comments:
------------------------------------------------------------------------
[2009-08-12 22:49:59] scottmac@php.net
Works for me here with 5.2-dev and 5.3-dev
object(stdClass)#1 (2) {
["test2"]=>
int(5)
["_empty_"]=>
int(6)
}
------------------------------------------------------------------------
[2009-08-12 21:20:22] michaeldnelson dot mdn at gmail dot com
I am using freebsd 7.2 and php 5.2.10 the first test case
var_dump(json_decode('{"":"test"}'));
silently stops processing of the script
The test case
var_dump(json_decode('{"test2":5,"":6}'));
Fatal error: Cannot access empty property
I am not sure the second is expected behavior but considering json is often from feeds it would be
nice to fail more gracefully.
------------------------------------------------------------------------
[2008-12-26 01:00:01] 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".
------------------------------------------------------------------------
[2008-12-23 16:47:05] matt at mpcm dot com
A note about this in the json_decode page would be appreciated. Perhaps a strict mode flag, so that
it can fail if it cannot be decoded as an object (?). For the moment the only safe way to decode
json is into arrays.
The _empty_ behavior I thought was a bug (as it was with arrays) is a required behavior for objects
(at least without __get && __set in a wrapper class).
The bugs that jump out at me now can be seen with the code below... you can create objects with
unreachable properties. If that blank property access is fixed, then this becomes not so much of an
issue. Or it should be stopped from ever happening... silent errors bite us all.
Illegal Member variable name if var_dump does it, fatal if a user does it.
Notice: PHPDocument1 line 8 - Illegal member variable name
<?
$blank = '';
$another = ' another';
$a = array($blank=>1234, 'some other key'=>5678, $another=>9999);
$b = (object) $a;
$c = json_decode(json_encode($a));
var_dump($b);
var_dump($c);
#echo $b->$blank; //fatal
echo $b->$another; //works
#echo $c->$blank; //fatal
echo $c->$another; //works
?>
------------------------------------------------------------------------
[2008-12-18 03:27:25] scottmac@php.net
I'm not even sure what the bug is? You can't have an empty property name hence the use of
"_empty_".
The key collision thing is a very edge case, are you saying you ran into this in a real life usage?
The best course of action may be to have this documented on the json_encode() and json_decode()
pages.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=46600
--
Edit this bug report at https://bugs.php.net/bug.php?id=46600&edit=1