How can I access an object attribute that starts with a number?

What about this :

$Beeblebrox->{'2ndhead'}

Actually, you can do this for pretty much any kind of variable — even for ones that are not class properties.

For example, you could think about a variable’s name that contains spaces ; the following syntax will work :

${"My test var"} = 10;
echo ${"My test var"};

Even if, obviously, you would not be able to do anything like this :

$My test var = 10;
echo $My test var;

No idea how it’s working internally, though… And after a bit of searching, I cannot find anything about this in the PHP manual.

Only thing I can find about {} and variables is in here : Variable parsing — but not quite related to the current subject…

But here’s an article that shows a couple of other possiblities, and goes farther than the examples I posted here : PHP Variable Names: Curly Brace Madness

And here’s another one that gives some additionnal informations about the way those are parsed : PHP grammar notes

Leave a Comment