显式定义 PHP 中多个未知参数的可能性


Explicitly define the possibility of multiple unknown arguments in PHP

我想创建一个可能需要多个未知参数的函数。我目前没有定义参数列表。函数定义如下所示

public static function SafeJoin()

然后我在函数中使用$arguments_list = func_get_args();将参数放入列表中。

这种方法的问题在于,没有显式的方法来知道函数可以接收多个参数。除了使用数组作为显式参数之外,是否有其他方法可以解决问题。

有。但在 PHP 版本 5.6 中添加。

function myFunction(...$params)
{
    echo '<pre>';
    print_r($params);
    echo '</pre>';
}

函数$params内部是简单的数组。你可以调用函数,例如:myFunction(1,'foo',250,$bar);

点击! 供参考