views:

1875

answers:

6

First question on SO and it's a real RTFM 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.

+3  A: 

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 quotes like this:

$class->'variable_name' // Same as $class->variable_name

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

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

James Skidmore
This isn't the situation in which $this->_session->{'user_id'} is being used.
jimyi
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
@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
@James: Yeah, auto-generated code. That makes sense.
David Weinraub
+5  A: 

I know the syntax just when using variable variables:

$userProp = 'id';
$this->_session->{'user_'.$userProp};
Gumbo
+3  A: 

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
+6  A: 

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
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
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
+3  A: 

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
+1  A: 

I just wrote a post on the issue of curly braces that might be of use to visitors of this page: http://grahamwideman.wordpress.com/2009/08/05/php-grammar-notes/