doulidai6316 2013-06-18 06:42
浏览 13
已采纳

如何从PHP下的另一个函数获取方法的参数列表?

If I have a class like this :

class A {
    public function method1($arg1, $arg2){}
}

and now, I need to do something like this :

/**
 * @return array The list of arguemnt names
 */
function getMethodArgList(){
    return get_method_arg_list(A, method1);
}

so, how could I implement the function getMethodArgList() ? any one could help me ?

  • 写回答

1条回答 默认 最新

  • doupingmao1903 2013-06-18 06:43
    关注

    Not quite sure if I get the question, but ReflectionClass and ReflectionMethod might be what you're looking for.

    e.g.

    <?php
    var_dump(getMethodArgList());
    
    class A {
        public function method1($arg1, $arg2){}
    }
    
    function getMethodArgList() {
        $rc = new ReflectionClass('A');
        $rm = $rc->getMethod('method1');
        return $rm->getParameters();
    }
    

    prints

    array(2) {
      [0] =>
      class ReflectionParameter#3 (1) {
        public $name =>
        string(4) "arg1"
      }
      [1] =>
      class ReflectionParameter#4 (1) {
        public $name =>
        string(4) "arg2"
      }
    }
    

    To get only the names you can use

    return array_map(function($e) { return $e->getName(); }, $rm->getParameters());
    

    instead of return $rm->getParameters();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB四叉树处理长方形tif文件
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B