php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52717 Numeric property name can be set/get via variable but not static
Submitted: 2010-08-27 11:55 UTC Modified: 2015-06-10 13:45 UTC
Votes:7
Avg. Score:3.4 ± 1.4
Reproduced:3 of 5 (60.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: epicfailmail at tempinbox dot com Assigned: cmb (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.3 OS: Windows 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: epicfailmail at tempinbox dot com
New email:
PHP Version: OS:

 

 [2010-08-27 11:55 UTC] epicfailmail at tempinbox dot com
Description:
------------
It is not possible to assign a numeric (integer or string) property name directly but via a variable.

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

$obj = new stdClass;
$zero_int = 0;
$zero_str = "0";

$obj->$zero_int = "bar"; // works
$obj->$zero_str = "bar"; // works
$obj->0 = "bar"; // Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in foo.php on line 9
$obj->"0" = "bar"; // Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$' in foo.php on line 10

print_r($obj->$zero_int); // works
print_r($obj->$zero_str); // works
print_r($obj->0); // Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' in foo.php on line 13
print_r($obj->"0"); // Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$' in foo.php on line 15

?>

Expected result:
----------------
I expected that both ways or neither of them would work.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-03 18:40 UTC] + at ni-po dot com
Use the complex variable syntax: $obj->{0} or $obj->{"0"} will work.
 [2015-06-10 13:45 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-06-10 13:45 UTC] cmb@php.net
> Use the complex variable syntax: $obj->{0} or $obj->{"0"} will
> work.

Exactly.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 20:01:29 2024 UTC