I need to know, how much arguments a function accepts (including optional args). w/o calling that function and w/o getting errors. Like I can check with is_callable before I call a function in order to not get php error.
For example, in javascript every function have a length property:
Math.sin.length == 1
Array.prototype.slice.length == 2
I know, this will return only declared arguments, but there’s no support for optional arguments with default values in js syntax.
Is there a similar thing in php?
Working in php 5.2+ is enough. Wroking for only global functions (not methods) is also enough.
Hackish solutions like trying to call function with 0 args, then with 1 arg… are highly discouraged, cause function call cost may be very high.
Use
ReflectionFunction:In PHP 5.3,
ReflectionFunctionsupports closures: