Search code examples
phpcodeigniter-2

How I can grab all public function methods in a controller?


I use __remap() function to avoid any undefine method and make it redirect to index() function.

function __remap($method)
{
   $array = {"method1","method2"};
   in_array($method,$array) ? $this->$method() : $this->index();
}

That function will check if other than method1 and method2.. it will redirect to index function.

Now, how I can automatically grab all public function methods in that controller instead of manually put on $array variable?


Solution

  • OK, I was bored:

    $r = new ReflectionClass(__CLASS__);
    $methods = array_map(function($v) {
                            return $v->name;
                         },
                         $r->getMethods(ReflectionMethod::IS_PUBLIC));