Tibi
Tibi

Reputation: 137

how to dynamically check number of arguments of a function in php

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

Upvotes: 12

Views: 5949

Answers (4)

Greg
Greg

Reputation: 321864

You need to use reflection to do that.

$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();

Upvotes: 31

Alana Storm
Alana Storm

Reputation: 166166

Reflection is what you're after here

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

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

Upvotes: 9

Andy Baird
Andy Baird

Reputation: 6228

I believe you are looking for func_num_args()

https://www.php.net/manual/en/function.func-num-args.php

Upvotes: -5

gnud
gnud

Reputation: 78628

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

Specifically, look at the ReflectionFunction and ReflcetionMethod classes.

Upvotes: 2

Related Questions