鉴于冗余,是否有更好的方法来做到这一点?如果是这样,它会是什么样子?传递可变参数列表
请注意第一个$method
参数,该参数作为函数名称在$api
类中传递,并且不能作为参数传递给API函数。其后每一个论证都可能不存在。
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);
}
你可以使用一个for循环? – twodayslate
那将是什么样子?我考虑过它,但是无法将我的头部用于将函数调用引发的语法。 –
你可以为扩展参数传递'func_get_arg(2)'或'NULL'吗? –