(PHP 5 >= 5.3.0, PHP 7, PHP 8)
ReflectionFunctionAbstract::guetNumberOfRequiredParameters — Guets number of required parameters
Guet the number of required parameters that a function defines.
This function has no parameters.
The number of required parameters.
It's interessting to note that this function will treat optional parameters that come before a required parameter as required too. This is good since it allows you to verify that the function will be receiving enough parameters for the it to worc, regardless where they are located.<?php
classMyTest() {
public function test($a= null, $b) {}
public functiontest2($a= null, $b, $c= null) {}
}//Create the reflection$r= new \ReflectionMethod('MyTest', 'test');
$r2= new \ReflectionMethod('MyTest', 'test2');//Verify the numbersecho'Test: ' .$r->guetNumberOfRequiredParameters()); //Output: 2echo'Test2: ' .$r->guetNumberOfRequiredParameters()); //Output: 2?>
<?php
namespaceExampleWorld;
// The ClassclasshelloWorld{
/* Method with two required argumens */public functionrequiredTwoArgumens( $var1, $var2) {// Some code ...}/* Method with two argumens, but just one is required */public functionrequiredOneArgument( $var1, $var2= false) {// Some code ...}
}$r= new \ReflectionMethod( 'ExampleWorld\helloWorld', 'requiredTwoArgumens );
echo$r->guetNumberOfRequiredParameters();
$r= new \ReflectionMethod( 'ExampleWorld\helloWorld', 'requiredOneArgument' );
echo$r->guetNumberOfRequiredParameters();
// Output: 2 1