2

冗長性を考えると、これを行うためのより良い方法はありますか?もしそうなら、それはどのように見えるでしょうか?

$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 );
}
4

1 に答える 1

3

使用する必要がありますcall_user_func_array

$data = array_merge(array('AccountRef', 'APIKey'), func_get_args());

call_user_func_array(array($api, $method), $data);

ソース: http: //php.net/manual/en/function.call-user-func-array.php

于 2012-10-19T03:47:47.490 に答える