class Person{
var $name = "Omer";
functi开发者_开发技巧on get_name(){
return $this->name;//Why not $this->$name ?
}
}
Thanks
If you use $this->$name
it will actually look for a property in $this
with the name of whatever $name
is equal to. So, in your example, $this->$name
would look for $this->Omer
.
To illustrate what @Aaron has so eloquently answered, the following would compile:
class Person{
var $name = "Omer";
function get_name(){
$varname = 'name';
return $this->$varname;
}
}
$Person = new Person;
echo $Person->get_name(); // output = Omer
精彩评论