Dealing with special characters in object property

2020-04-19 07:39发布

问题:

I am working with a database created by a university professor's intern. Many of the fields have names like 'Revenues_(budget)'.

Currently when working with objects that have the fields as properties I do something like

$f = 'Revenues_(budget)';
echo $obj->$f;

This works fine but I was wondering if there might be a more elegant or at least concise way to handle these?

回答1:

You can do that in one expression:

echo $obj->{'Revenues_(budget)'};


标签: php oop