想写一则API共通函数API正常调用是这样的 
$api->a(x1,x2) 
$api->b(x1,x2,x3,x4,x5)我想写一个共通函数d($fun,参数){
  $api->$fun(参数)
}参数那个地方如何处理  请高手赐教

解决方案 »

  1.   

    php库函数:
    func_get_arg
    func_get_args
    func_num_args
      

  2.   

    http://php.net/manual/en/function.call-user-func.phpcall_user_func
    (PHP 4, PHP 5)call_user_func — Call a user function given by the first parametermixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )
    Call a user defined function given by the function parameter. 
      

  3.   

    function d(){
      static $api;
      if(!$api)
      {
        $api  = new api();
      }
      $args = func_get_args();
      $fun  = array_shift($args);
      if(method_exists( $api,$fun))
      {
       call_user_func_array(array($api,$fun),$args);
      }
      else 
      {
       echo "You are calling a unknow method '{$fun}'";
      }
    }class api
    {
    function a($x1,$x2)
    {
    $args  = func_get_args();
    echo implode(' ', $args);
    }

    function b($x1,$x2,$x3,$x4,$x5)
    {
    $args  = func_get_args();
    echo implode(' ', $args);
    }
    }d('a','hello','world');
    echo "<br/>";
    d('b','hey','dude','are','you','there?');
    echo "<br/>";
    d('c','???');
      

  4.   

    calss api {
      function d() {
        $p = func_get_args();
        $fun = array_unshift($p);
        if( method_exists($this, $fun))
          return call_user_func_array(array($this, $fun), $p);
        if(function_exists($fun))
          return call_user_func_array($fun, $p);
        exit("方法(函数) $fun 未定义");
      }
    }