Please excuse me if this question has been asked before, but I tried searching for it with no satisfactory results.
I'm learning PHP (coming from a C++ background) and have come across the following ambiguity. The following two bits of code work exactly the same:
class A
{
public $myInteger;
public function __get($name)
{
return $this->$name;
}
public function __set($name, $value)
{
$this->$name = $value;
}
}
and
class A
{
public $myInteger;
public function __get($name)
{
return $this->name;
}
public function __set($name, $value)
{
$this->name = $value;
}
}
that is, in the class methods $this->$name
and $this->name
have the exact same function. I'm finding this a bit confusing, especially when considering that if you add the following code,
$myA = new A();
$myA->myInteger = 5;
$hereInt = $myA->myInteger;
echo "<p>" . $hereInt . "</p>";
it only works if there is no $
before myInteger
. Could someone please explain the rationale behind this?
$this->$name
and$this->name
do not mean the same thing. The first is using a locally scoped variable$name
to access the field of$this
whose name is whatever$name
contains, while the second accesses thename
field directly.For example, the following will output
something
:In the case of
__get
and__set
,$name
contains the name of the property that was accessed at the call site; in your case,myInteger
.In your example, the
__get
and__set
methods are actually superfluous, since$myA->myInteger
is public and can be accessed directly.__get
and__set
are only needed to catch access attempts to a property that is not declared explicitly in the class.For example, you might have a backing array that allows arbitrary "properties" to be set dynamically:
One thing that's somewhat confusing about this aspect of PHP's syntax is that a
$
precedes a field declaration in a class, but there is none when accessing that field. This is compounded by the syntax for accessing static fields, which does require a$
!