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
 [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 09:01:24 2020 UTC