Check if class has method in PHP

后端 未结 4 1400
你的背包
你的背包 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...

提交回复
热议问题