0

I am wondering if you can get the argument length of a php functions arguments from an external scope.

$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

If I var_dump(test) I can see a property on the Closure of properties but I can't get at it.

There is func_num_args(void) but that only works from inside the function scope.

I have tried...

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

2 Answers 2

3

You'll want to reflect on the function:

$ref = new ReflectionFunction($test);
echo $ref->getNumberOfParameters();
Sign up to request clarification or add additional context in comments.

Comments

1

code:

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

    var_dump($length); // => 2
};

var_dump($test);

result:

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

if return function argument length, You can try ReflectionFunction

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.