PHP curly brace syntax for member variable

36,678

Solution 1

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

Solution 2

I know the syntax just when using variable variables:

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

Solution 3

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

Solution 4

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

Solution 5

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;
Share:
36,678

Related videos on Youtube

David Weinraub
Author by

David Weinraub

Phuket-based web developer. Primary interests include PHP, MySQL, MongoDb, Zend Framework, Slim, Composer, Doctrine, Node.js, jQuery, OO design and principles, MVC, modeling, separation of concerns, unit-testing, loose coupling, dependency injection. Oh, and astronomy and cosmology. SOreadytohelp

Updated on July 05, 2022

Comments

  • David Weinraub
    David Weinraub almost 2 years

    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.

    • David Weinraub
      David Weinraub almost 15 years
      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.
    • Rolf
      Rolf almost 10 years
      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");
    • David Weinraub
      David Weinraub almost 10 years
      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)
  • jimyi
    jimyi almost 15 years
    This isn't the situation in which $this->_session->{'user_id'} is being used.
  • David Weinraub
    David Weinraub almost 15 years
    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?
  • mercator
    mercator almost 15 years
    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"...
  • Kent Fredric
    Kent Fredric almost 15 years
    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.
  • James Skidmore
    James Skidmore almost 15 years
    @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.
  • Bob Stein
    Bob Stein about 11 years
    +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.