I am trying to call a method stored as $_auto, but it will not work.
<?php
class Index {
private $_auto;
public function __construct() {
$this->_auto = "index";
$this->_auto();
}
public function index() {
echo "index";
}
}
$index = new Index();
?>
You need to use
call_user_functo do this: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
__callto your class:This will allow you to invoke callables, so you can call free functions:
And class methods:
And of course you can customize this behavior by tweaking what
__callinvokes.