Kikimac
Kikimac

Reputation: 179

PHP Anonymous object - integer properties

I've created an anonymous object, using PDO FETCH_OBJ from my DB. I can access most properties fine using:

$myObject->name;  
$myObject->age;

etc.

But I have one field in my DB that starts with an integer. '130x90_url

When trying to access $myObject->130x90_url; I then get :

syntax error, unexpected '130' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

I can't see anything in the manual about this, but would of thought others would of come across this issue?

Upvotes: 0

Views: 152

Answers (2)

thecoolestname36
thecoolestname36

Reputation: 114

You might also be able to assign the property name to a variable and use that as a property.

$property = "130x90_url";
$value = $myObject->$property;

Upvotes: 1

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

Try this:

echo $myObject->{'130x90_url'};

That works on SimpleXMLElement instances, and should work on instances of stdClass.
More details can, probably, be found on the variable variables doc page.

Upvotes: 6

Related Questions