php 在调用 call_user_func() 之前检查类中是否存在函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19537423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:40:05  来源:igfitidea点击:

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

php

提问by S.Visser

In the following code I call a class with 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';
}

Let's say that the controller has the following code.

假设控制器具有以下代码。

class test {

    static function test_function() {
        echo 'test';
    }

}

When I call call_user_func('test', 'test_function')there isn't any problem. But when I call a function that does not exist, it does not work. Now I want to check first if the function in the class testdoes exist, before I call the function call_user_func.

当我打电话call_user_func('test', 'test_function')时没有任何问题。但是当我调用一个不存在的函数时,它不起作用。现在我想先检查类test中的函数是否存在,然后再调用函数call_user_func

Is there a function that checks if a function exists in an class or is there another way I can check this?

是否有一个函数可以检查类中是否存在某个函数,或者还有其他方法可以检查这个函数吗?

回答by Elias Van Ootegem

You're looking for method_existsfor starters. But what you shouldcheck, too is whether or not the method is callable. This is done by the helpfully named is_callablefunction:

您正在寻找method_exists初学者。但是您还应该检查该方法是否是callable。这是由有用的命名is_callable函数完成的:

if (method_exists($this->controller, $this->view)
    && is_callable(array($this->controller, $this->view)))
{
    call_user_func(
        array($this->controller, $this->view)
    );
}

But that's just the start of things. Your snippet contains explicit requirecalls, which suggests you're not using an autoloader.
What's more: all you're doing is check file_exists, not if the class was already loaded. Your code, then, will generate a fatal error if, per chance your snippet gets executed twice with the same values for $this->controller.
Begin fixing this by, in the very least, changing your requireto require_once...

但这只是事情的开始。您的代码段包含显式require调用,这表明您没有使用autoloader
更重要的是:您所做的只是 check file_exists,而不是类是否已经加载。那么,如果您的代码片段每次使用相同的$this->controller.
开始解决这个问题,至少,改变你requirerequire_once......

回答by ProGM

You can use the PHP function method_exists():

您可以使用 PHP 函数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...);

回答by GiuServ

From PHP 5.3, you can also use:

从 PHP 5.3 开始,您还可以使用:

if(method_exists($this, $model))
    return forward_static_call([$this, $model], $extra, $parameter);

回答by Steyx

Use method_exists($this->controller, $this->view). For your example:

使用method_exists($this->controller, $this->view). 对于您的示例:

if(file_exists('controller/' . $this->controller . '.controller.php') && 
   method_exists($this->controller,$this->view)) {

    require('controller/' . $this->controller . '.controller.php');
    call_user_func(array($this->controller, $this->view));

} else {
    echo 'error: controller or function not exists <br/>'. 'controller/' . $this->controller . '.controller.php';
}