我使用__remap()
函数来避免任何undefine方法并使其重定向到index()函数。
function __remap($method)
{
$array = {"method1","method2"};
in_array($method,$array) ? $this->$method() : $this->index();
}
该函数将检查除了method1和method2之外是否会重定向到索引函数。
现在,我如何自动抓取该控制器中的所有公共功能方法,而不是手动放入$array
变量?
答案 0 :(得分:1)
您需要测试方法是否存在且是否公开。所以你需要使用反射和方法存在。像这样:
{{1}}
或者您可以使用get_class_methods()来创建方法数组
答案 1 :(得分:0)
$r = new ReflectionClass(__CLASS__);
$methods = array_map(function($v) {
return $v->name;
},
$r->getMethods(ReflectionMethod::IS_PUBLIC));
答案 2 :(得分:0)
我修改了代码并变成了这样。
function _remap($method)
{
$controllers = new ReflectionClass(__CLASS__);
$obj_method_existed = array_map(function($method_existed)
{
return $method_existed;
},
$controllers->getMethods(ReflectionMethod::IS_PUBLIC));
$arr_method = array();
//The following FOREACH I think was not good practice.
foreach($obj_method_existed as $method_existed):
$arr_method[] = $method_existed->name;
endforeach;
in_array($method, $arr_method) ? $this->$method() : $this->index();
}
任何增强功能,而不是使用 foreach ?