Get amount of function arguments OUTSITE in php
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.
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 on开发者_如何转开发ly 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
:
function a($b, $c = 1) {
echo $b, $c;
}
$rf = new ReflectionFunction('a');
echo $rf->getNumberOfParameters();
In PHP 5.3, ReflectionFunction
supports closures:
$a = function($b, $c = 1) {
echo $b, $c;
};
$rf = new ReflectionFunction($a);
echo $rf->getNumberOfParameters();
精彩评论