Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取函数可以接受的参数总数_Php_Function_Oop_Parameters_Arguments - Fatal编程技术网

Php 获取函数可以接受的参数总数

Php 获取函数可以接受的参数总数,php,function,oop,parameters,arguments,Php,Function,Oop,Parameters,Arguments,我有一个函数,它有两个参数: function index($LoadFunction, $footer) { // My statements } 我可以获得发送到此函数的参数总数,如下所示索引(1,2,3,4) 我想知道允许我向这个index()函数发送多少个参数 我怎样才能做到这一点 EDIT1: 我也试过了,但没有任何反应 EDIT2: 好的,我会用更恰当的方式解释: class foo { function bar ( arg1, arg2 ){ .....

我有一个函数,它有两个参数:

function index($LoadFunction, $footer) {
    // My statements
}
我可以获得发送到此函数的参数总数,如下所示
索引(1,2,3,4)

我想知道允许我向这个
index()
函数发送多少个参数

我怎样才能做到这一点

EDIT1:

我也试过了,但没有任何反应

EDIT2:

好的,我会用更恰当的方式解释:

class foo { function bar ( arg1, arg2 ){ ..... } } 现在我想做的是:

$method = new ReflectionClass('foo', 'bar'); $num = $method->getNumberOfParameters(); // Ideally this should give me total number of arguments which this 'bar' // function can take, but it won't echo anything.
以上是来自

的参数数量几乎是无穷无尽的。限制并不取决于语言本身,而是取决于硬件/操作系统的限制,如内存使用等。因此,您可能不会遇到真正会导致任何问题的情况

您也可以将数组作为参数传递,而不是无止境的参数列表,数组本身也可以包含无止境的键/值对列表。在资源使用方面不会有太大的区别,但我个人认为,当参数列表可能非常长时,处理起来更容易

--编辑

如果我正确理解你想要做什么(但如果我不理解,请更清楚地解释自己),这就是你想要的:

function my_function($arg1, $arg2) {
    if(func_num_args() > 2) {
        throw new ErrorException('Maximum number of arguments exceeded');
        // OR: trigger_error('Maximum number of arguments exceeded', E_USER_ERROR);
    }

    echo 'You are inside "my_function()" and have passed exactly 2 arguments';
}
执行此操作时:

my_function('some value', 'another value');
它回显:
您在“my_function()”中,并且正好传递了2个参数

但当您执行此代码时:

my_function('some value', 'another value', 'too much arguments...');
它将抛出异常(或触发错误,无论您选择什么)

--编辑2

是的,现在我明白了;)

首先:尝试在方法上使用ReflectionClass。您应该使用ReflectionFunction(对于过程函数)或ReflectionMethod(对于类方法)

备选案文1:

function my_function($arg1, $arg2) {
    // do something
}

$reflection = new ReflectionFunction('my_function');
echo 'Function "my_function" has '. $reflection->getNumberOfParameters() .' arguments';
备选案文2:

class MyClass {
    function my_function($arg1, $arg2) {
        // do something
    }
}

$reflection = new ReflectionMethod('MyClass::my_function');
echo 'Class method MyClass::my_function() has '. $reflection->getNumberOfParameters() .' arguments';
在这两种情况下,您都可以使用
$reflection->getNumberOfRequiredParameters()

PHP支持用户定义的可变长度参数列表 功能。这非常简单,使用func_num_args(), func_get_arg()和func_get_args()函数

不需要特殊语法,参数列表可能仍然是 显式提供函数定义,其行为如下 正常

因此,您可以将任意数量的参数传递给函数
索引(1,2,3,4,5,…)
在函数中,如果未传递至少2个参数,PHP将生成警告


检查

@zerkms嘿,这很好,我已经通过了,但无法完成。你能给我一些关于如何使用这个的想法吗?检查链接的第一条评论可能重复的@bansi与你给出的不一样。在运行函数之前,我想检查一个函数接受的参数总数
必需参数
。是的,我明白了,但我的问题是:如果我发送两个参数,那么我将调用该函数,如果不发送,我将显示一个错误。因此,如何做到这一点是我的querywell函数的足迹,因为它现在是两个参数是强制性的。当没有参数或只给出一个参数时,它本身会显示一个错误(除非您给它一个默认值)。其他参数(参数3+)被忽略(除非您显式使用它们)。如果你想抛出一个错误,只要
my_function('some value', 'another value', 'too much arguments...');
function my_function($arg1, $arg2) {
    // do something
}

$reflection = new ReflectionFunction('my_function');
echo 'Function "my_function" has '. $reflection->getNumberOfParameters() .' arguments';
class MyClass {
    function my_function($arg1, $arg2) {
        // do something
    }
}

$reflection = new ReflectionMethod('MyClass::my_function');
echo 'Class method MyClass::my_function() has '. $reflection->getNumberOfParameters() .' arguments';