简体   繁体   English

具有无限参数的类方法

[英]class method with unlimited parameters

I have the following code: 我有以下代码:

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

As you can see, the second and third parameter to calling the Arrays::without function. 如您所见,调用Arrays::without函数的第二个和第三个参数。 you can pass as many parameters as you want, such as: 您可以根据需要传递任意多个参数,例如:

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

Im trying to encapsulate this into a static method inside a class i have: 我试图将其封装到我有的类中的静态方法中:

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

What i need to know is if there is a way i can pass unlimited arguments to this method without and use them inside my funciton call to Arrays::without 我需要知道的是,是否有一种方法可以将无限制的参数传递给该方法without在我对Arrays::without函数调用中使用它们

You can call: 您可以致电:

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

Where $p is an array or write your method like that: 其中$p是一个数组或像这样编写您的方法:

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

But it's quite unnecessary. 但这是完全不必要的。

Check splat operator: http://php.net/manual/en/migration56.new-features.php#migration56.new-features.splat 检查splat运算符: http : //php.net/manual/en/migration56.new-features.php#migration56.new-features.splat

在函数内部使用func_get_args() ,或者在public static function without(...$args)中使用函数签名...。所有这些都在PHP Docs中进行了描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM