在下面的代码中,我调用了一个类call_user_func()
。在调用call_user_func()之前检查函数中是否存在函数
if(file_exists('controller/' . $this->controller . '.controller.php')) {
require('controller/' . $this->controller . '.controller.php');
call_user_func(array($this->controller, $this->view));
} else {
echo 'error: controller not exists <br/>'. 'controller/' . $this->controller . '.controller.php';
}
可以说控制器具有以下代码。
class test {
static function test_function() {
echo 'test';
}
}
当我打电话call_user_func('test', 'test_function')
没有问题。但是,当我调用一个不存在的函数时,它不起作用。现在我想首先检查te类测试中的函数是否存在,然后再调用函数call_user_func
。
是否有一个函数,检查是否在一个类中存在一个函数或有其他方式,我可以检查这个?
[method_exists()](http://php.net /manual/en/function.method-exists.php) –
谢谢@MarkBaker! –
不是每个现有的方法都可以调用 - > [is_callable](http://php.net/manual/en/function.is-callable.php) – a4c8b