Check if class has method in PHP

后端 未结 4 1409
你的背包
你的背包 2021-02-12 16:04

Currently my code looks like that:

switch ($_POST[\'operation\']) {
    case \'create\':
        $db_manager->create();
        break;
    case \'retrieve\':
         


        
相关标签:
4条回答
  • 2021-02-12 16:09

    You can use method_exists:

    if (method_exists($db_manager, $_POST['operation'])){
      $db_manager->{$_POST['operation']}();
    } else {
      echo 'error';
    }
    

    Though I strongly advise you don't go about programming this way...

    0 讨论(0)
  • 2021-02-12 16:14

    Use method_exists()

    method_exists($obj, $method_name);
    
    0 讨论(0)
  • 2021-02-12 16:19

    You can use is_callable() or method_exists().

    The difference between them is that the latter wouldn't work for the case, if __call() handles the method call.

    0 讨论(0)
  • 2021-02-12 16:31

    You can use method_exists(). But this is a really bad idea

    If $_POST['operation'] is set to some magic function names (like __set()), your code will still explode. Better use an array of allowed function names.

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