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 Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
5 + 17 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Tue Apr 16 21:01:28 2024 UTC