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->$nameand$this->namedo not mean the same thing. The first is using a locally scoped variable$nameto access the field of$thiswhose name is whatever$namecontains, while the second accesses thenamefield directly.For example, the following will output
something:In the case of
__getand__set,$namecontains the name of the property that was accessed at the call site; in your case,myInteger.In your example, the
__getand__setmethods are actually superfluous, since$myA->myIntegeris public and can be accessed directly.__getand__setare 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$!