13

I have a PHP Object with an attribute having a dollar ($) sign in it.

How do I access the content of this attribute ?

Example :

echo $object->variable; // Ok

echo $object->variable$WithDollar; // Syntax error :-(
kevin
  • 1,138
  • 2
  • 15
  • 32
  • Try to do this first: `vardump ($object);`, then examine the result. – Donny Kurnia Jan 19 '10 at 12:06
  • 2
    If it's coming from an SQL query, perhaps you should rename that field in the SQL (i.e., 'select variable$withDollar as varwithoutdollar'). – JAL Jan 19 '10 at 13:38
  • @Code Duck : Yep I could have, that's what I did until now, I was trying to find out if it was possible directly in PHP. – kevin Jan 19 '10 at 14:02
  • I'd say that the solution Code Duck gave in his comment is really the only reasonable one. Writing, reading, and maintaining code that's peppered with invalid variable names and hacky code to get around such things is going to be unnecessarily time-consuming and will lead to bugs and other unforeseen problems, each of which will in turn require more hacks. Solve the problem as close to its source as possible. – GZipp Jan 19 '10 at 15:52
  • As I said in one of my comments below, I don't have any choice at all. The system I have to query is 20 years old and you can't change stuff like that on the fly. I completly agree with you, in general the best way to do it would have been to fix the source of the problem but I've found a non-hacky way to do what I wanted to do. It's not a problem any more. – kevin Jan 20 '10 at 09:12
  • Related: [How do I access this object property with a hyphenated name?](http://stackoverflow.com/q/758449/367456) – hakre Dec 31 '13 at 21:03

5 Answers5

42
  1. With variable variables:

    $myVar = 'variable$WithDollar';
    echo $object->$myVar;
    
  2. With curly brackets:

    echo $object->{'variable$WithDollar'};
    
nickf
  • 537,072
  • 198
  • 649
  • 721
  • you mean, with string variables? – Luca Matteis Jan 19 '10 at 11:48
  • No, variable variables is the correct term. But $ is illegal in variable names ... – Jan Hančič Jan 19 '10 at 11:49
  • 1
    @Jan, that doesn't change the fact that you can get a variable or a property with a $ in the name... – nickf Jan 19 '10 at 11:52
  • Oh, that seems to work ! Is there any way not to use a second variable ($myVar) ? something like : echo $object->'variable$WithDollar';) – kevin Jan 19 '10 at 11:58
  • @Kevin: I pointed out how to do that in my answer. But what are you trying to achieve in the first place? A mySQL result set with a $ in the field name is odd to say the least. – Pekka Jan 19 '10 at 12:03
  • @Pekka : It's not a MySQL result, a database adapter builds the results of a SQL query into an array of objects. The table I'm querying has a weird schema and some columns have dollar signs in their name. – kevin Jan 19 '10 at 13:15
  • @kevin: I see. Well, both nickf's, mstiles's and my approach work for that - pick your choice. – Pekka Jan 19 '10 at 13:24
  • Is there any name/documentation for the curly braces syntax? – mvorisek Nov 13 '19 at 13:40
4

Thanks to your answers, I just found out how I can do that the way I intended :

echo $object->{'variable$WithDollar'}; // works !

I was pretty sure I tried every combination possible before.

kevin
  • 1,138
  • 2
  • 15
  • 32
3

I assume you want to access properties with variable names on the fly. For that, try

echo $object->{"variable".$yourVariable}
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

There are reflection methods that also allow you to construct method and attribute names that may be built by variables or contain special characters. You can use the ReflectionClass::getProperty ( string $name ) method.

$object->getProperty('variable$WithDollar');

mark stiles
  • 183
  • 2
  • 11
1

You don't.

The dollar sign has a special significance in PHP. Although it is possible to bypass the variable substitution in dereferencing class/object properties you NEVER should be doing this.

Don't try to declare variables with a literal '$'.

If you're having to deal with someoneelse's mess - first fix the code they wrote to remove the dollars then go and chop off their fingers.

C.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • 1
    Some of the software that primarly runs the database is 20 years old. Believe me I would love to just have readable columns names (and not AF$AT1 for example), I'm communicating with the database and can't change anything on it ! – kevin Jan 19 '10 at 13:45