3

I'm new to OOP in PHP and I find the difference between the following two expressions difficult to understand.

    $object->$foo;
    $object->foo;

Maybe it's my fault, but I could not find the relevant part in the manual.

3
  • It's ClassName::$foo for accessing static properties and $object->foo for accessing object members Commented Feb 16, 2014 at 9:56
  • @RoyalBg, Correct. But OP was confused with where to put the $ symbol. Commented Feb 16, 2014 at 10:03
  • @RoyalBg Thanks for the extra info about static properties. Commented Feb 16, 2014 at 10:20

2 Answers 2

2

The first call $obj->$foo is using a so called variable variable. Check this:

class A { 
    public $foo = 1;
}

$a = new A();
$foo = 'foo';

// now you can use both
echo $a->$foo;
echo $a->foo;

Follow the manual about variable variables

Sign up to request clarification or add additional context in comments.

2 Comments

Nice one hek, Perfect. +1
Thanks :) I must admit that my first, sunday morning, look also told me that the first line is wrong ;)
2

Well, in order to fully understand the somewhat odd-looking $object->$foo, you should understand two things about PHP:

Variable names

Most of the time variables in PHP are quite straight-forward. They begin with a $ sign, have one [a-zA-Z_] character, and then any amount of [a-z-A-Z0-9_] characters. Examples include:

$var  = 'Abcdef';
$_GET = [];
$a1   = 123;
// And so on...

Now, PHP variables can actually be named pretty much anything, as long as the name is, or can be cast to, a scalar type. The way you name a variable with anything is to use curly braces ({}), like this:

${null}  = 'It works'; echo ${null};
${false} = 'It works'; echo ${false};
${'!'}   = 'It works'; echo ${'!'};

// Slightly weirder...
${(int)trim(' 5 ')}       = 'It works'; echo ${5};
${implode(['a','b','c'])} = 'It works'; echo $abc;

Important: Just because you can do this does not mean you should, however. It is mostly just an oddity of PHP that you can do this.

Variable variables

A somewhat convoluted explanation: A variable variable is a variable that is accessed using a variable name.

A much easier way to understand variable variables is to use what we just learning about variable names in PHP. Take this example:

${"abc"} = 'Abc...';
echo $abc;

We create a variable using the string, "abc", which can also be accessed using $abc.

Now, there is no reason (or rule) that says it has to be a string.... it can also be a variable:

$abc = 'Abc...';
$varName = 'abc';
echo ${$varName}; // echo $abc

That is basically a variable variable. "Real" variable variables just do not use the curly braces:

$abc = 'Abc...';
$varName = 'abc';
echo $$varName; // echo $abc

As for the question

In the question the $object->$foo thing is basically just an "object variable variable", if you like

$object = new stdClass;
$object->abc = 'The alphabet!';

$foo = 'abc';
echo $object->$foo;
echo $object->{$foo};  // The same
echo $object->{'abc'}; // The same

Object variable variables can be somewhat useful, but they are rarely necessary. Using an associative array is usually a better choice.

1 Comment

Thanks for extending the view. Very useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.