Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Get argument count

This was incredibly surprising to see that PHP has no obvious function to do what I'm looking for, so I'll ask here.

How would you go about getting the number of arguments a particular function has outside of the function (just like func_num_args only from outside the function).

The solution can't actually execute the function (that would be defeating the purpose), and I'd like to do it (preferably) without any sort of reflection class.

Possible?

like image 565
Qix - MONICA WAS MISTREATED Avatar asked Dec 07 '25 20:12

Qix - MONICA WAS MISTREATED


1 Answers

Oh, you can use the ReflectionFunction class, which inherits from ReflectionFunctionAbstract, which defines the getNumberOfParameters function:

$func_reflection = new ReflectionFunction('function_name');
$num_of_params = $func_reflection->getNumberOfParameters();

Note: This will only work on user functions, not class or instance functions.

like image 96
Jacob Relkin Avatar answered Dec 10 '25 11:12

Jacob Relkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!