PHP: Hypertext Preprocessor (original) (raw)
ReflectionFunctionAbstract::getNumberOfRequiredParameters
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
ReflectionFunctionAbstract::getNumberOfRequiredParameters — Gets number of required parameters
Description
public ReflectionFunctionAbstract::getNumberOfRequiredParameters(): int
Parameters
This function has no parameters.
Return Values
The number of required parameters.
Found A Problem?
9 years ago
`It's interesting 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 work, regardless where they are located.
b,b, b,c = null) {} }//Create the reflection $r = new \ReflectionMethod('MyTest', 'test'); $r2 = new \ReflectionMethod('MyTest', 'test2');//Verify the numbers echo 'Test: ' . $r->getNumberOfRequiredParameters()); //Output: 2 echo 'Test2: ' . $r->getNumberOfRequiredParameters()); //Output: 2?>`
sebastian at sebastian-eiweleit dot de ¶
11 years ago
<?php namespace ExampleWorld; // The Class class helloWorld { /* Method with two required arguments */public function requiredTwoArguments ( <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>r</mi><mn>1</mn><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">var1, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord">1</span><span class="mpunct">,</span></span></span></span>var2 ) { // Some code ... }/* Method with two arguments, but just one is required */ public function requiredOneArgument ( <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>v</mi><mi>a</mi><mi>r</mi><mn>1</mn><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">var1, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8389em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord">1</span><span class="mpunct">,</span></span></span></span>var2 = false ) { // Some code ... } }$r = new \ReflectionMethod ( 'ExampleWorld\helloWorld', 'requiredTwoArguments' ); echo <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mo>−</mo><mo>></mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>N</mi><mi>u</mi><mi>m</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>O</mi><mi>f</mi><mi>R</mi><mi>e</mi><mi>q</mi><mi>u</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>d</mi><mi>P</mi><mi>a</mi><mi>r</mi><mi>a</mi><mi>m</mi><mi>e</mi><mi>t</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo><mo separator="true">;</mo></mrow><annotation encoding="application/x-tex">r->getNumberOfRequiredParameters ();</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.10903em;">tN</span><span class="mord mathnormal">u</span><span class="mord mathnormal">mb</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord mathnormal" style="margin-right:0.02778em;">O</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.03588em;">q</span><span class="mord mathnormal">u</span><span class="mord mathnormal">i</span><span class="mord mathnormal">re</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">am</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ers</span><span class="mopen">(</span><span class="mclose">)</span><span class="mpunct">;</span></span></span></span>r = new \ReflectionMethod ( 'ExampleWorld\helloWorld', 'requiredOneArgument' ); echo $r->getNumberOfRequiredParameters ();// Output: 2 1