On 25 March 2016 at 21:19, Scott Arciszewski wrote:
On Fri, Mar 25, 2016 at 1:45 PM, Nikita Popov wrote:

Hi internals,

Currently we do not allow (*) creating empty property names on objects,

$obj->{''} = 42;

is illegal. While empty property names are unlikely to be useful per se,
they are problematic for deserialization of foreign formats like JSON. To
avoid this issue {"": null} will currently decode to a property named
"_empty_" rather than "". Notably, this means that JSON decode and encode
do not round-trip (as we do not convert _empty_ back to an empty name while

There is no technical reason (that I can see) for keeping this arbitrary
restriction. I believe that the original reason for the restriction was our
use of NUL-prefixed property names for name mangling, combined with the
fact that an empty string at the C level happens to "start" with a NUL

A patch to drop the restriction and allow empty property names:
https://github.com/php/php-src/pull/1836 It does not touch the JSON
handling, as there are BC concerns involved there, I leave that to the
ext/json maintainer.

Any objections to changing this?


(*) There are roundabout ways to create them anyway.
​Currently, this succeeds:

$x = json_decode('{"_empty_": "no", "": "foo"}', true);

While this does not:

$x = json_decode('{"_empty_": "no", "": "foo"}');


I'm personally 50/50 on it. ​I think allowing an empty property is kind of
weird, but not the weirdest behavior PHP allows. Overall, it might
(ironically enough!) make working with JSON _more_ consistent, and probably
have other benefits that I can't even imagine at the moment.
Note that also that these ones won't work at the moment:

var_dump((object) ['' => 'foo']);
var_dump((object) ["\0*\0" => 'foo']);
var_dump((object) ["\0Foo\0" => 'foo']);

Seems to work consistently on HHVM though: https://3v4l.org/7MO7E

Marco Pivetta



Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 12 | next ›
Discussion Overview
groupphp-internals @
postedMar 25, '16 at 5:45p
activeMay 29, '16 at 7:23p



site design / logo © 2019 Grokbase