I’m trying to build an AJAX wrapper in CodeIgniter
function admin_ajax () {
$model = $this->input->post('model');
$method = $this->input->post('method');
if ( empty($model) || empty($method) )
die('Missing Class or Method');
if ( !method_exists($model, $method) )
die('Class or Method does not exist');
$args = $this->input->post('args');
$data = $this->$model->$method( $args );
echo json_encode( $data );
exit();
}
You can probably tell what I’m trying to do. POST the model and method to this function along with some arguments, this function then calls the function and passes the arguments ($args is an array btw).
Suppose I have a function like:
get_site_list ($page_index = 0, $page_size = 10) {
It takes two arguments, but my ajax function which calls it is only capable of passing a single argument. I’ve had to revert to doing
function ajax_get_site_list ( $args ) {
return $this->get_site_list( $args[0], $args[1] );
}
but that’s boring. Is there a way I can take my array of arguments called $args and pass them to a function which accepts multiple arguments?
I think you’re looking for
call_user_func_array:You could also get really fancy with
ReflectionClass😉