How do I access a PHP object attribute having a dollar sign?
Solution 1:
-
With variable variables:
$myVar = 'variable$WithDollar'; echo $object->$myVar;
-
With curly brackets:
echo $object->{'variable$WithDollar'};
Solution 2:
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.
Solution 3:
I assume you want to access properties with variable names on the fly. For that, try
echo $object->{"variable".$yourVariable}
Solution 4:
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.