More tough love from PHP
Consider the following:
class Foo
{
private $var = 'avalue';
private function doStuff()
{
}
}
As you might expect, you can’t access $var or doStuff() from outside Foo, which is well and good. However, things get strange when you throw overloading into the mix.
class Foo
{
private $var = 'avalue';
private function doStuff()
{
}
public function __get($var)
{
return $this->$var;
}
}
$foo = new Foo;
var_dump($foo->var);
This works; if a variable isn’t accessible from the calling scope, it will invoke __get() instead. Too bad it’s not consistent:
class Foo
{
private $var = 'avalue';
private function doStuff()
{
}
public function __get($var)
{
return $this->$var;
}
public function __call($func, array $args = array())
{
return call_user_func_array(array($this, $func), $args);
}
}
$foo = new Foo;
var_dump($foo->doStuff());
This breaks with: “Fatal error: Call to private method Foo::doStuff() from context ”.”
It would be really nice if PHP followed the same rules for overloaded methods as for overloaded variables.
