PHP: get the number of parameters required by a function

This is a PHP extension issue pass to $ this for an outer class function

And I believe that this is what I am looking for, but it is in python, not php: Programmatically determining the number of parameters a function needs - Python

Say I have a function like this:

function client_func($cls, $arg){ } 

and when I am ready to call this function, I can do something like this in pseudocode:

 if function first parameter equals '$cls', then call client_func(instanceof class, $arg) else call client_func($arg) 

So, in principle, there is a way to look at the function and see what parameter values ​​are needed before calling the function.

I assume this will be similar to debug_backtrace, but vice versa.

func_get_args () can only be called inside a function that doesn't help me here.

Any thoughts?

+6
source share
2 answers

Use Reflection , especially ReflectionFunction in your case.

 $fct = new ReflectionFunction('client_func'); echo $fct->getNumberOfRequiredParameters(); 

As far as I can see, you will also find getParameters ()

+15
source

Only path with reflection by going to http://us3.php.net/manual/en/book.reflection.php

 class foo { function bar ($arg1, $arg2) { // ... } } $method = new ReflectionMethod('foo', 'bar'); $num = $method->getNumberOfParameters(); 
0
source

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


All Articles