12

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

4 Answers 4

31

You need to use reflection to do that.

$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();
Sign up to request clarification or add additional context in comments.

1 Comment

9

Reflection is what you're after here

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

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

Comments

2

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

Specifically, look at the ReflectionFunction and ReflcetionMethod classes.

Comments

-5

I believe you are looking for func_num_args()

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

2 Comments

No. This is for use inside functions, and says how many arguments were passed to the function you are in.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.