How to check if a function is callable with parameters?

I call the function using call_user_func_array:

call_user_func_array(array($this, 'myFunction'), array('param1', 'param2', 'param3'));

Everything is fine, if I do not know how many parameters a function requires. If a function needs 4 parameters, it sends me an error, I would like to check if I can call the function (with an array of parameters). is_callable () does not allow checking parameters. Edit: if the call fails, I need to call another function, so I need a check.

Thanks!

+3
source share
2 answers

You can use reflection to get the number of parameters:

$refl = new ReflectionMethod(get_class($this), 'myFunction');
$numParams = $refl->getNumberOfParameters();

or

$numParams = $refl->getNumberOfRequiredParameters();

See here for more information.

+8
source

- . PHP , , , .

. func_get_args().

: crescentfresh, , . ( ) , :

Warning: Wrong parameter count for strpos() in Command line code on line [...]
+1

Source: https://habr.com/ru/post/1702506/


All Articles