Reputation: 2224
I'm trying to dynamically select a property from an object, but I'm not sure how to accomplish this.
$prop = '12345';
$object->$prop
in effect trying to recreate this:
$object->12345
Upvotes: 1
Views: 53
Reputation: 68476
You need to use the curly braces if you want to access that way..
$myobject = new stdClass;
$prop = '12345';
$myobject->$prop = $prop;
echo $myobject->{12345}; //"prints" 12345
or simply echo $myobject->$prop
will do.
If you access it as echo $myobject->12345;
, below error will be thrown.
PHP Parse error: syntax error, unexpected '12345' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
Upvotes: 1