php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70127 var_dump((object)array("123" => new stdclass, "123" => "muh"));
Submitted: 2015-07-24 12:40 UTC Modified: 2016-08-12 12:22 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: office at moving-bytes dot at Assigned: cmb (profile)
Status: Duplicate Package: Scripting Engine problem
PHP Version: 5.6.11 OS: *
Private report: No CVE-ID: None
 [2015-07-24 12:40 UTC] office at moving-bytes dot at
Description:
------------
Usually its not possible to create class and variablenames containing only numbers.
But if you create an array and typecast it to object, these rules does not exist anymore. 
 

Test script:
---------------
var_dump((object)array("123" => new stdclass, "123" => "muh"));


Expected result:
----------------
PHP Parse error:  syntax error, unexpected '123' (T_LNUMBER), expecting identifier (T_STRING) 

Actual result:
--------------
object(stdClass)#1 (1) {
  [123]=>
  string(3) "foo"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-24 13:05 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Package: PHP Language Specification +Package: Scripting Engine problem -Operating System: +Operating System: * -Assigned To: +Assigned To: cmb
 [2015-07-24 13:05 UTC] cmb@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

It is possible to have numeric property names by other means:

    $o = new StdClass();
    $o->{123} = 'foo';
    var_dump($o);
    
This is a feature, and not a bug.
 [2015-07-24 13:40 UTC] office at moving-bytes dot at
-: office at moving-byte dot at +: office at moving-bytes dot at
 [2015-07-24 13:40 UTC] office at moving-bytes dot at
imho this is a bug, since this is a complete inconsistent behavior:

example from stackoverflow:

$a = array('123' => '123');
$o1 = (object)$a;
$o2 = new stdClass;
$o2->{'123'} = '123'; // setting property is OK

echo $o1->{'123'}; // error!
echo $o2->{'123'}; // works... WTF?

http://stackoverflow.com/questions/10333016/how-to-access-object-properties-with-names-like-integers/10333200#10333200
 [2015-07-24 14:00 UTC] bwoebi@php.net
IMHO, we really ought to fix that.

arrays store and access (rightfully, I guess) numeric keys as integers.
objects store and access all the keys as strings.

We maybe should do the same numeric access for dynamic properties on objects...
 [2015-07-24 14:01 UTC] bwoebi@php.net
-Status: Not a bug +Status: Analyzed
 [2015-07-24 14:07 UTC] laruence@php.net
I don't think so, it will be overhead while casting or accessing for rarely cases..

just with a good document should be fine.
 [2015-07-24 14:07 UTC] laruence@php.net
I don't think so, it will be overhead while casting or accessing for rarely cases..

just with a good document should be fine.
 [2015-07-24 14:22 UTC] cmb@php.net
The behavior when casting object to array is already well
documented[1]:

> If an object is converted to an array, the result is an array
> whose elements are the object's properties. The keys are the
> member variable names, with a few notable exceptions: integer
> properties are unaccessible; [...]

[1] <http://php.net/manual/en/language.types.array.php#language.types.array.casting>
 [2015-07-24 14:42 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2015-07-24 15:26 UTC] bwoebi@php.net
@laruence: note that the overhead will only exist for dynamic property accesses (non-CONST VM operand and only on a dynamic access - meaning when there is no fast access to property table). Which should be a rare enough case … and that particular case is already relatively slow.

I think we could afford it?
 [2016-08-12 12:22 UTC] cmb@php.net
-Status: Analyzed +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-08-12 12:22 UTC] cmb@php.net
Duplicate of bug #66173.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 13:01:24 2019 UTC