Given the redundancy, is there a better way to do this? If so, what would it look like?
Note the first $method argument, which is passed as a function name within the $api class, and must not be passed as an argument to the API function. Every argument thereafter might not exist.
function jsonRequest( $method )
{
$api = new JsonRpcClient( 'https://api.example.com' );
switch ( func_num_args() ) {
case 2:
$result = $api->$method( 'AccountRef', 'APIKey', func_get_arg( 1 ) );
break; case 3:
$result = $api->$method( 'AccountRef', 'APIKey', func_get_arg( 1 ), func_get_arg( 2 ) );
break; case 4:
$result = $api->$method( 'AccountRef', 'APIKey', func_get_arg( 1 ), func_get_arg( 2 ), func_get_arg( 3 ) );
break; case 5:
$result = $api->$method( 'AccountRef', 'APIKey', func_get_arg( 1 ), func_get_arg( 2 ), func_get_arg( 3 ), func_get_arg( 4 ) );
break;
}
return json_decode( $result );
}
You should use
call_user_func_array:Source: http://php.net/manual/en/function.call-user-func-array.php