You need to use call_user_func
to do this:
call_user_func(array($this, $this->_auto));
Unfortunately PHP does not allow you to directly use property values as callables.
There is also a trick you could use to auto-invoke callables like this. I 'm not sure I would endorse it, but here it is. Add this implementation of __call
to your class:
public function __call($name, $args)
{
if (isset($this->$name) && is_callable($this->$name)) {
return call_user_func_array($this->$name, $args);
}
else {
throw new \Exception("No such callable $name!");
}
}
This will allow you to invoke callables, so you can call free functions:
$this->_auto = 'phpinfo';
$this->_auto();
And class methods:
$this->_auto = array($this, 'index');
$this->_auto();
And of course you can customize this behavior by tweaking what __call
invokes.