Bug #67300 [Opn->Csd]: Cast (object) array() ALLOWS to create property with empty name
| From: | nikic@php.net | Date: | Sat, 19 Nov 2016 17:37:06 +0000 |
| Subject: | Bug #67300 [Opn->Csd]: Cast (object) array() ALLOWS to create property with empty name | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-205475@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67300&edit=1
ID: 67300
Updated by: nikic@php.net
Reported by: vovan-ve at yandex dot ru
Summary: Cast (object) array() ALLOWS to create property with
empty name
-Status: Open
+Status: Closed
Type: Bug
Package: Class/Object related
PHP Version: Irrelevant
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
As per the previous comment, empty object property name are supported everywhere as of PHP 7.1.
Previous Comments:
------------------------------------------------------------------------
[2016-07-02 20:04:42] Husman85 at gmail dot com
This has been fixed in PHP 7.1+ See: https://github.com/php/php-src/pull/699
------------------------------------------------------------------------
[2014-05-19 06:41:41] vovan-ve at yandex dot ru
Description:
------------
Due to some reasons we are not allowed to create property with empty name:
$object = new stdClass();
$object->{""} = 42;
// Fatal error: Cannot access empty property
Ok. But type-cast
(object) doesn't know about this restriction: http://3v4l.org/IU3GW . This inconsistency cause some problems.
All PHP/5 versions affected including PHP/5.5 and PHP/5.6.0beta3:
Some features emits a notice, others aren't.
Some features interacts with empty property, others aren't.
(there are some different behaviours in very old versions)
Test script:
---------------
<?php
$object = (object) array(
"foo" => 42,
"" => 37,
"bar" => 19,
);
echo "+ object created:\n";
var_dump($object);
echo "\nenum properties:\n";
foreach ($object as $p => $v) {
var_dump($p);
}
echo "\nget_object_vars()\n";
var_dump(get_object_vars($object));
Expected result:
----------------
Fatal error: Cannot access empty property
(or an Exception should be better)
Actual result:
--------------
+ object created:
object(stdClass)#1 (3) {
["foo"]=>
int(42)
Notice: Illegal member variable name in ... on line 9
[""]=>
int(37)
["bar"]=>
int(19)
}
enum properties:
string(3) "foo"
Notice: Illegal member variable name in ... on line 12
string(3) "bar"
get_object_vars()
Notice: Illegal member variable name in ... on line 17
array(2) {
["foo"]=>
int(42)
["bar"]=>
int(19)
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67300&edit=1