82

First question on SO and it's a real RTM candidate. But I promise you I've looked and can't seem to find it. I'll happily do a #headpalm when it turns out to be a simple thing that I missed.

Trying to figure out Zend Framework and came across the following syntax:

$this->_session->{'user_id'}

I have never seen the curly braces syntax used to access what appears to be a member variable. How is it different than

$this->_session->user_id

I'm assuming that the _session is irrelevant, but including it in the question since it may not be.

Are the curly braces just a cleanliness convention that attempts to wrap the compound variable name user_id? Or is it some kind of a special accessor?

Any pointers into TFM so I can R up would be humbly appreciated.

Many thanks. Please be gentle.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • 7
    Thanks all, for the quick and excellent answers. With this gentle initiation into SO, I am encouraged to use it more. Hopefully, I can help answer questions, not just ask 'em. Cheers. – David Weinraub Jul 20 '09 at 08:42
  • 1
    Interesting tidbit: You can call methods with the curly syntax, and the result is as fast as calling the method directly. Using call_user_func is more general, but it takes twice as long to do call_user_func(array($obj,$method),"parm1","parm2") instead of $obj->{$method}("parm1,"parm2"); – Rolf Jul 20 '14 at 15:45
  • And the latter is more readable, too. At least IMO. Actually, I often omit the curly braces on the method name in that case: `$obj->$method($params)` – David Weinraub Jul 21 '14 at 07:35

5 Answers5

56

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."

jmikola
  • 6,892
  • 1
  • 31
  • 61
James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • 2
    This isn't the situation in which $this->_session->{'user_id'} is being used. – jimyi Jul 18 '09 at 16:28
  • Thanks for the quick reply. Yeah, I'll take the headpalm. I've used the syntax in other circumstances, just didn't recognize it in the context of member variables. Even worse, in my search I was on that page in the manual, but didn't see the member variable sample. Sigh... Still, the example I gave was kind of an odd case in which to use it, right? No real ambiguity, no real delineation necessary. Certainly syntactically valid, but kind of unnecessary usage. See what I mean? – David Weinraub Jul 18 '09 at 16:36
  • 2
    @jimyi, I clarified my answer further. Thanks. @papayasoft, I agree that it is pretty unnecessary in your case. My guess is that it's auto-generated code and they use the complex curly syntax just to cover all bases. – James Skidmore Jul 18 '09 at 18:51
25

I know the syntax just when using variable variables:

$userProp = 'id';
$this->_session->{'user_'.$userProp};
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 1
    +1 for the only answer with a proper link to TFM, as @jimyi hinted. The O.P. is not about the complex (curly) syntax for strings. It's about the use of curly braces to resolve ambiguities in variable variables. – Bob Stein Mar 13 '13 at 02:51
20

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 }"} 
Kent Fredric
  • 56,416
  • 14
  • 107
  • 150
  • 6
    It doesn't have to be as extreme as your examples, though. For example, you could imagine having that session data stored in a database, and a column named "user-id" instead of "user_id"... – mercator Jul 18 '09 at 16:39
  • 2
    Also, on older versions of PHP putting \0 at the front of the string ISTR let you access pivate variables. Now at least \0 gives you a "you cant do that sorry" fatal exception. – Kent Fredric Jul 18 '09 at 16:42
13

In the example you give, there's no real difference, and IMO $this->_session->user_id should be used because it's clearer.

What the curly brace syntax is actually good for is accessing a member variable by constructing an expression for its name, like $this->_session->{'user_id' . $index}.

chaos
  • 122,029
  • 33
  • 303
  • 309
11

The two examples in your question do the same thing. PHP allows you to access member data/methods in several ways...

object->{'name_of_member'};

object->name_of_member;

$member = 'name_of_member';
object->$member;
Donnie DeBoer
  • 2,517
  • 15
  • 14