©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 5 >= 5.0.3)
ReflectionFunctionAbstract::getNumberOfRequiredParameters — 获取必须输入参数个数
获取函数定义中,必须输入的参数个数
本函数还未编写文档,仅有参数列表。
此函数没有参数。
必须输入的参数个数
[#1] cesar at magic3w dot com [2015-07-07 12:45:21]
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.
<?php
class MyTest() {
public function test($a = null, $b) {}
public function test2($a = null, $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
?>
[#2] sebastian at sebastian-eiweleit dot de [2013-09-02 15:03:56]
<?php
namespace ExampleWorld;
// The Class
class helloWorld {
public function requiredTwoArguments ( $var1, $var2 ) {
// Some code ...
}
public function requiredOneArgument ( $var1, $var2 = false ) {
// Some code ...
}
}
$r = new \ReflectionMethod ( 'ExampleWorld\helloWorld', 'requiredTwoArguments' );
echo $r->getNumberOfRequiredParameters ();
$r = new \ReflectionMethod ( 'ExampleWorld\helloWorld', 'requiredOneArgument' );
echo $r->getNumberOfRequiredParameters ();
// Output: 2 1