Reputation: 5924
I know you can use func_num_args
to get the number of actual arguments passed in. Is there any way to get the number of formal parameters?
Example:
<?php
function optional($a, $b = null) {
echo func_num_args();
}
optional(1); // 1
optional(1, 1); // 2
optional(1, 1, 1); // 3
I want a way to get 2
in each of those calls to optional
, without hard-coding it.
Upvotes: 1
Views: 89
Reputation: 214949
One way is with ReflectionFunction
, e.g.:
function optional($a, $b = null) {
return (new ReflectionFunction(__FUNCTION__))->getNumberOfParameters();
}
Upvotes: 3