PHP curly brace syntax for member variable PHP curly brace syntax for member variable php php

PHP curly brace syntax for member variable


Curly braces are used to explicitly specify the end of a variable name. For example:

echo "This square is {$square->width}00 centimeters broad."; 

So, your case is really a combination of two special cases. You're allowed to access class variables using curly braces and like so:

$class->{'variable_name'} // Same as $class->variable_name$class->{'variable' . '_name'} // Dynamic values are also allowed

And in your case, you're just surrounding them with the curly brace syntax.

See the PHP manual, "complex (curly) syntax."


I know the syntax just when using variable variables:

$userProp = 'id';$this->_session->{'user_'.$userProp};


Theres probably one big advantage of that syntax, however, its generally in the domain of hairy stuff, and things you probably want to avoid.

It permits you to use characters in variable names that are otherwise unpermitted.

ie:

$this->object->{"hello world\0\n"} $this->object->{"function(){   this is a truely awful  name for a variable }"}