|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65585 Cannot access empty property : Why not ?
Submitted: 2013-08-29 17:04 UTC Modified: 2013-08-31 10:00 UTC
From: kylekatarnls at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.19 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
23 + 27 = ?
Subscribe to this entry?

 [2013-08-29 17:04 UTC] kylekatarnls at gmail dot com
Why can we not access property with empty name ?

I wish be able to do something like this :

$object->{""} = "Foo";
echo $object->{""};

Actualy, we can do :

$array = (array) $object;
$array[""] = "Foo";
echo $array[""];
$object = (object) $array;

So why could we not set and get the value of an property with empty name ?

Test script:
$object->{""} = "Foo";
echo $object->{""};

Expected result:
print Foo and throw any error or just a notice.

Actual result:
Fatal error : Cannot access empty property


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-30 20:55 UTC]
-Status: Open +Status: Not a bug
 [2013-08-30 20:55 UTC]
This is by design: the fix for bug #52484 made empty properties inaccessible to 
avoid silent data loss when an empty property was set in __set().

At any rate, I'm not sure there's a good argument for supporting empty properties 
— object properties and array keys aren't strictly analogous in PHP, and casting 
between arrays and objects can result in data loss in other ways as it is.
 [2013-08-31 10:00 UTC] kylekatarnls at gmail dot com
However a object with empty property names can exist. So why if a do :
$object = (object) [ '' => 'Foo' ];
There is no error. And if I do this :
$object = new stdClass;
$object->{''} = 'Foo';
There is a Fatal error for the same result ?

Do not this codes should both throw the same error ? A warning sound good in my opinion.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon May 17 14:01:24 2021 UTC