Check if a function exists in a class before calling call_user_func()

后端 未结 4 778
青春惊慌失措
青春惊慌失措 2020-12-14 16:35

In the following code I call a class with call_user_func().

if(file_exists(\'controller/\' . $this->controller . \'.controller.php\')) {
             


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 17:02

    You can use the PHP function method_exists():

    if (method_exists('ClassName', 'method_name'))
    call_user_func(etc...);
    

    or also:

    if (method_exists($class_instance, 'method_name'))
    call_user_func(etc...);
    

提交回复
热议问题