php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67300 Cast (object) array() ALLOWS to create property with empty name
Submitted: 2014-05-19 06:41 UTC Modified: 2016-11-19 17:37 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: vovan-ve at yandex dot ru Assigned: nikic (profile)
Status: Closed Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vovan-ve at yandex dot ru
New email:
PHP Version: OS:

 

 [2014-05-19 06:41 UTC] 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)
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-02 20:04 UTC] Husman85 at gmail dot com
This has been fixed in PHP 7.1+ See: https://github.com/php/php-src/pull/699
 [2016-11-19 17:37 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2016-11-19 17:37 UTC] nikic@php.net
As per the previous comment, empty object property name are supported everywhere as of PHP 7.1.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC