tags:

views:

392

answers:

4

how can I check at runtime home many parameters a method or a function have in PHP.

example

class foo {
   function bar ( arg1, arg2 ){
    .....
   }
}

I will need to know if there is a way to run something like

get_func_arg_number ( "foo", "bar" )

and the result to be

2
A: 

You're looking for the reflection capabilities in PHP5 -- documentation here.

Specifically, look at the ReflectionFunction and ReflcetionMethod classes.

gnud
A: 

I believe you are looking for func_num_args()

http://us3.php.net/manual/en/function.func-num-args.php

Andy Baird
No. This is for use inside functions, and says how many arguments were passed to the function you are in.
gnud
That will return the number of arguments passed to the function when you've called it. The OP is looking for the number of arguments in the function signature, which may be different.
JW
+9  A: 

You need to use reflection to do that.

$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();
Greg
+3  A: 

Reflection is what you're after here

class foo {
   function bar ( $arg1, $arg2 ){

   }
}
$ReflectionFoo = new ReflectionClass('foo');
echo $ReflectionFoo->getMethod('bar')->getNumberOfParameters();
Alan Storm