tags:

views:

107

answers:

2

I am wondering what is the difference between $this->name and $this->$name? Also does $this have to be strictly named this or can it be anything?

+15  A: 

$this is a reserved variable name and can not be used for anything else. It specifically points you to the object your are currently working in. You have to use $this because you do not know what variable object will be assigned to.

$this->name refers to the current class's variable name

$this->$name refers to the class variable of whatever the value of $name is. Thus

$name = "name";
echo $this->$name; // echos the value of $this->name.

$name = "test";
echo $this->$name;  // echos the value of $this->test
Chacha102
+6  A: 

$this is a reserved name used in PHP to point to the current instance of the class you are using it in (quoting) :

The pseudo-variable $this is available when a method is called from within an object context.
$this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).


When using $this->name, you are accessing the property with the name name of the current object.


When using $this->$name, $name is determined before accessing the property -- which means you'll access the property which name is contained in the $name local variable.

For instance, with this portion of code :

$name = 'abc';
echo $this->$name;

You'll actually echo the content of the abc property, as if you had written :

echo $this->abc;

When doing this, you are using variable variables (quoting) :

Class properties may also be accessed using variable property names.
The variable property name will be resolved within the scope from which the call is made.
For instance, if you have an expression such as $foo->$bar, then the local scope will be examined for $bar and its value will be used as the name of the property of $foo.
This is also true if $bar is an array access.

Pascal MARTIN
You have an error in your syntax. Mixing single quotes with double quotes are we? (Tried to fix it, and you overwrote me)
Chacha102
@Chacha : ergh, once again, too early in the morning ^^ thanks for noticing that :-) -- huhu, we both edited my answer to correct that problem, I see :-D ;; and we edited our comments at the same time, too :-D
Pascal MARTIN
If we were programming, we'd be sunk as StackOverflow doesn't have comment checkouts/locks :)
Chacha102
If we were programming, we would either use transactions, or have some conflicts when trying to commit our modifications ^^ *(well, at least for the answer's edition)*
Pascal MARTIN
I think someone has suggested that to Jeff. Maybe he'll listen to it after hes stopped telling himself how good the sliding comment timer is...
Chacha102
It's not **that bad** either : I don't quite that often edit at the same time another user does -- and for comments, it's not a catastrophy either (and being able to edit comments helps fixing the situations in which cross-posting happens)
Pascal MARTIN