K
K
khodos_dmitry2020-05-25 21:10:53
PHP
khodos_dmitry, 2020-05-25 21:10:53

Is it possible in PHP to determine how many arguments a function requires?

There is a function:

function func($arg1, $arg2) {
}
args_func(func); // должно вернуть 2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-05-25
@khodos_dmitry

https://www.php.net/manual/ru/class.reflectionfunc...

function func($arg1, $arg2 = null) {}

$refFunc = new ReflectionFunction('func');
print $refFunc->getNumberOfParameters(); // 2
print $refFunc->getNumberOfRequiredParameters(); // 1

R
rPman, 2020-05-25
@rPman

It is impossible to determine the number of parameters from out of context, because since version 5.6 support for a variable number of arguments has appeared variadic functions
Inside you can - func_num_args
ps so the best option is to parse the sources

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question