Is there a way to check if a function exists within a class?

后端 未结 3 1709
小鲜肉
小鲜肉 2020-12-20 01:02

I\'m passing some post data to execute a function based on post data, to determine if this should execute I\'ve tried to use the following:

$SP = new StoredP         


        
相关标签:
3条回答
  • 2020-12-20 01:14

    method_exists checks for method of a class for a given object:

    Docs Link: http://www.php.net/method_exists

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

    function_exists() and method_exists() are for these checks. First is for regular functions and second for OOP functions.

    0 讨论(0)
  • 2020-12-20 01:16

    You should use method_exists

    Try with:

    if(method_exists($SP, $_POST['function'])) {
    
    0 讨论(0)
  • 2020-12-20 01:17

    check this all

    Find out if a method exists in a static class

    Checking if function exists

    and also PHP manual at

    php.net/method_exists

    php.net/manual/en/function.function-exists.php

    www.php.net/class_exists

    Hope these might help you.

    0 讨论(0)
提交回复
热议问题