具有无限参数的类方法



我有以下代码:

$array = array('foo', 'foo', 'bar', 'bis', 'ter')
Arrays::without($array, 'foo', 'bis') // Returns array('bar', 'ter')

如您所见,调用Arrays::without函数的第二个和第三个参数。 您可以根据需要传递任意数量的参数,例如:

Arrays::without($array, 'foo', 'bis','athirdparam','afourthparam') // Returns array('bar', 'ter')

试图将其封装到我拥有的类中的静态方法中:

public static function without($arr,$p)
{
    return Arrays::without($arr,$p);
}

我需要知道的是,是否有办法将无限的参数传递给此方法without并在函数调用中使用它们Arrays::without

你可以调用:

Arrays::without($arr, ...$p);

其中$p是一个数组或像这样编写方法:

public static function without($arr,$p)
{
    return Arrays::without($arr, ...$p);
}

但这完全没有必要。

检查板操作符:http://php.net/manual/en/migration56.new-features.php#migration56.new-features.splat

要么

在函数内部使用 func_get_args(),要么public static function without(...$args)用于函数签名。这一切都在 PHP 文档中进行了描述

最新更新