php.net |  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
 [2013-08-29 17:04 UTC] kylekatarnls at gmail dot com
Description:
------------
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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-30 20:55 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2013-08-30 20:55 UTC] aharvey@php.net
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-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC