php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68186 A stdClass shouldn't allow invalid proprieties names.
Submitted: 2014-10-08 13:40 UTC Modified: 2014-10-08 22:59 UTC
From: ceceldada at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ceceldada at gmail dot com
New email:
PHP Version: OS:

 

 [2014-10-08 13:40 UTC] ceceldada at gmail dot com
Description:
------------
A stdClass shouldn't allow invalid proprieties names.

Numbers are not valid propriety name.

$object = new stdClass();
$object->0 = 1; //This is invalid
$object->{"\0"} = 1; //This is valid, why???

Test script:
---------------
<?php

$data = array( 'a', 'asdas' => 2312 );

print_r( (object) $data );

Expected result:
----------------
stdClass Object
(
    [asdas] => 2312
)

Actual result:
--------------
stdClass Object
(
    [0] => a
    [asdas] => 2312
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-08 22:59 UTC] leigh@php.net
-Status: Open +Status: Not a bug
 [2014-10-08 22:59 UTC] leigh@php.net
While it is not valid to define a property name with a leading digit in a class definition, the braced syntax in your example is a perfectly legal way of creating these kinds of properties.

As an example, if these properties were illegal we wouldn't be able to work with JSON:

$obj = json_decode('{"0": "hi"}');
var_dump($obj->{0}); // string(2) "hi"

Thanks for taking the time to report this though.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Aug 09 17:01:23 2020 UTC