The client (JS) has several methods that it can use when sending to the server by AJAX ('method =' + methodValue).

The server (PHP), in turn, processes $ _REQEST (because data can be sent by the POST method and GET). Depending on the method, the script must execute one or another code.

Question: How to organize the check of $ _REQEST for the presence of a 'method'a, if it is not known which method will be used for?

  • Look towards call_user_func php.net/manual/ru call_user_func ... and example number 4 ....... call_user_func(array('className', 'methodName')); - calls method methodName in class className ..... parameters can also be passed - Alexey Shimansky

1 answer 1

If the name of the method is passed in the parameter method, then you need to check the presence of the specified method in the class and, if it exists, call it

 class Action { public function method1($arg) { echo 'method1:'.$arg; } public function method2($arg) { echo 'method2:'.$arg; } } $act = new Action(); if (isset($_REQUEST['method']) && method_exists($act, $_REQUEST['method'])) { $act->{$_REQUEST['method']}('value'); }