【问题标题】:php get function argument lengthphp获取函数参数长度
【发布时间】:2016-12-15 09:39:09
【问题描述】:

我想知道您是否可以从外部范围获取 php 函数参数的参数长度。

$test = function($one, $two) {
  // this will work inside the scope of the function
  $length = func_num_args();
};
// I would like to access it from outside the functions scope
$length = get_arg_count(test); // => 2

如果我 var_dump(test) 我可以在 properties 的闭包上看到一个属性,但我无法获得它。

func_num_args(void),但只能在函数范围内工作。

我试过了……

test->properties;
test->properties();
test::properties;
test::properties();
test['properties'];

【问题讨论】:

    标签: php function arguments


    【解决方案1】:

    你会想reflect这个函数:

    $ref = new ReflectionFunction($test);
    echo $ref->getNumberOfParameters();
    

    【讨论】:

      【解决方案2】:

      代码:

      $test = function($one, $two) {
          // this will work inside the scope of the function
          $length = func_num_args();
      
          var_dump($length); // => 2
      };
      
      var_dump($test);
      

      结果:

      Press ENTER or type command to continue
      object(Closure)#1 (1) {
        ["parameter"]=>
        array(2) {
          ["$one"]=>
          string(10) "<required>"
          ["$two"]=>
          string(10) "<required>"
        }
      }
      

      如果返回函数参数长度,可以试试ReflectionFunction

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-22
        • 2015-04-10
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 2011-08-27
        • 2012-12-03
        相关资源
        最近更新 更多