how to determine number of arguments for a closure/anonymous function in PHP

anonymous-function, closures, php

Solution

Use reflection. See this article: http://www.bossduck.com/2009/07/php-5-3-closures-and-reflection/

$func = function($one, $two = 'test') {
    echo 'test function ran'.PHP_EOL;
};
$info = new ReflectionFunction($func);
var_dump(
    $info->getName(), 
    $info->getNumberOfParameters(), 
    $info->getNumberOfRequiredParameters()
);

Which returns:

string(9) "{closure}"
int(2)
int(1)

Problem

How can I determine the number of arguments that a closure is declared with for use outside of the closure? for example: ``` $myClosure = function($arg1, $arg2, $arg3){ } $numArgs = someMagicalFunction($myClosure); echo("that closure expects $numArgs arguments"); ``` Is there some function that does what I need?

Original source