Edit report at https://bugs.php.net/bug.php?id=65585&edit=1
ID: 65585
User updated by: kylekatarnls at gmail dot com
Reported by: kylekatarnls at gmail dot com
Summary: Cannot access empty property : Why not ?
Status: Not a bug
Type: Feature/Change Request
Package: *General Issues
PHP Version: 5.4.19
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2013-08-30 20:55:36] [email protected]
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-29 17:04:28] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65585&edit=1