2013-12-11 63 views
2

我傳遞了一些數據後根據交數據來執行的功能,以確定這是否應該執行我試圖使用以下命令:有沒有辦法檢查一個類中是否存在函數?

$SP = new StoredProcedure(); 

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

很不幸,這傳遞了以下錯誤:

Notice: Undefined property: StoredProcedure::$getFormList in C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\qcore\qcore_waitress.php on line 353 function does not exist.

我敢肯定,這個功能的確存在,當我沒有function_exists()

執行它有沒有一種方法來檢查,如果當它是一個類內部存在的功能?

回答

3

method_exists檢查一類的方法給定對象:

文檔鏈接: http://www.php.net/method_exists

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

function_exists()method_exists()是用於這些檢查。首先用於常規功能,其次用於OOP功能。

3

您應該使用method_exists

與嘗試:

if(method_exists($SP, $_POST['function'])) { 
+0

非常好!非常感謝你! – Codingo

相關問題