Reputation: 927
I'm reading this ebook, Beginning PHP5 and Mysql: From Novice to Professional, and in the OOP section, I'm trying out this sample code to reproduce the same results on my computer vs the book.
class Staff
{
var $name;
var $city;
protected $wage;
function __get($propName)
{
echo "__get called!<br />";
$vars = array("name","city");
if (in_array($propName, $vars))
{
return $this->$propName;
} else {
return "No such variable!";
}
}
}
$employee = new Staff();
$employee->name = "Mario";
echo $employee->name."<br />";
echo $employee->age;
In the book - the results are shown as:
Mario
__get called!
No such variable!
But on my computer:
Mario
Only the first line. The other two lines were "ignored". Why is that?!?!
Is there some configuration setting on my php.ini that I need to modify to get this working? Can someone please help to explain?
Upvotes: 0
Views: 123
Reputation: 18859
__get()
will only get called for non-public or non-existant properties. Now, there is a property called name
, so your magic method won't get called. Change var $name
into private $name
and it will work.
Upvotes: 1
Reputation: 16348
OK I think I found the answer. According to php docs
All overloading methods must be defined as public.
so make the magic method public
public function __get() {}
Upvotes: 0