|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 08:00:01 2025 UTC |
Use the complex variable syntax: $obj->{0} or $obj->{"0"} will work.> Use the complex variable syntax: $obj->{0} or $obj->{"0"} will > work. Exactly.