【问题标题】:Get total number of arguments a function can take获取函数可以接受的参数总数
【发布时间】:2013-07-15 20:16:49
【问题描述】:

我有一个带有两个参数的函数:

function index($LoadFunction, $footer) {
    // My statements
}

我可以像这样index(1,2,3,4) 得到发送给这个函数的参数总数

$numargs = func_num_args();
echo "Number of arguments: $numargs\n";

我想知道我可以向这个index() 函数发送多少个参数。

我将如何做到这一点。

EDIT1:

我也试过这些,但这并没有回应任何东西

https://stackoverflow.com/a/346789/1182021

https://stackoverflow.com/a/346943/1182021

EDIT2:

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

类富 { 功能栏(arg1,arg2){ ...... } }

我这样称呼foo类:

$class= 新的 foo(); // 实例化我的类 $class->bar(1,2); // 这段代码没问题

现在我想做的是:

$method = new ReflectionClass('foo', 'bar'); $num = $method->getNumberOfParameters(); // 理想情况下,这应该给我这个'bar'的参数总数 // 函数可以接受,但它不会回显任何内容。

所以我怎么能得到这个,我不想进入函数来检查它:I want to check it before executing the function.

我如何使用这个东西:

ReflectionFunctionAbstract::getNumberOfParameters — 获取参数数量 ReflectionFunctionAbstract::getNumberOfRequiredParameters — 获取所需参数的数量

以上来自http://php.net/manual/en/book.reflection.php

【问题讨论】:

标签: php function oop parameters arguments


【解决方案1】:

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

不需要特殊的语法,参数列表仍然可以 显式提供了函数定义,并将表现为 正常。

所以你可以将任意数量的参数传递给你的函数index(1,2,3,4,5,...) 在您的函数中,如果您没有传递至少 2 个参数,PHP 会生成一个警告。

查看http://php.net/manual/en/functions.arguments.php

【讨论】:

    【解决方案2】:

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

    您也可以只传递一个数组作为参数来代替无穷无尽的参数列表,它本身也可以保存几乎无穷无尽的键/值对列表。在资源使用方面不会有很大的不同,但我个人觉得当参数列表可能真的很长时更容易处理。

    -- 编辑

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

    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');
    

    它回显:You are inside "my_function()" and have passed exactly 2 arguments

    但是当你执行这段代码时:

    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()

    【讨论】:

    • 是的,我明白了,但我的问题是:如果我发送两个参数,那么我将调用该函数,否则我将显示错误。那么如何做到这一点是我的查询
    • 很好地使用函数足迹,因为它现在是两个参数是强制性的。当没有给出或只有一个参数时,它本身会显示一个错误(除非你给它一个默认值)。其他参数(参数 3+)将被忽略(除非您明确使用它们)。如果你想抛出错误,只需throw an exceptiontrigger an error
    猜你喜欢
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 2014-05-09
    相关资源
    最近更新 更多