有没有办法检查类中是否存在函数?

Mic*_*l A 2 php function

我正在传递一些发布数据以执行基于发布数据的函数,以确定这是否应该执行我尝试使用以下内容:

$SP = new StoredProcedure();

if(function_exists($SP->$_POST['function']))
{
    $SP->$_POST['function']();
}
else
{
    echo 'function does not exist.';
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这通过了以下错误:

注意:未定义属性:StoredProcedure::$getFormList in C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\qcore\qcore_waitress.php 353 行函数不存在。

我确定这个函数确实存在,当我在没有 function_exists()

有没有办法检查一个函数在类中是否存在?

Jas*_*n W 5

method_exists 检查给定对象的类的方法:

文档链接:http : //www.php.net/method_exists

if(method_exists($SP, $_POST['function'])) {
    {
        $SP->$_POST['function']();
    }
    else
    {
        echo 'function does not exist.';
    }
Run Code Online (Sandbox Code Playgroud)

function_exists()method_exists()用于这些检查。第一个用于常规函数,第二个用于OOP函数。