当我在服务器上安装Magento 2时,我得到了一个错误。在调查代码后,发现有三个点(…),这是产生错误。我附上了我在下面找到的代码:
return new $type(...array_values($args));
这个运算符叫什么,它的目的是什么?
当我在服务器上安装Magento 2时,我得到了一个错误。在调查代码后,发现有三个点(…),这是产生错误。我附上了我在下面找到的代码:
return new $type(...array_values($args));
这个运算符叫什么,它的目的是什么?
当前回答
要使用此特性,只需警告PHP需要使用…将数组解压缩为变量。操作符。更多细节请看这里,一个简单的例子可以是这样的:
$email[] = "Hi there";
$email[] = "Thanks for registering, hope you like it";
mail("someone@example.com", ...$email);
其他回答
这就是字面上的意思。PHP中的splat操作符,但在其他语言中被称为splat操作符。2014年LornaJane的一篇博客文章中提到:
这个特性允许您捕获一个函数的可变数量的参数,如果您愿意,还可以结合传入的“普通”参数。最简单的例子是: 函数连接($transform,…$strings) { $string = "; Foreach ($strings作为$piece) { $string .= $piece; } 返回(转换美元($ string)); } echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");
(这将打印我想要6个苹果)
函数声明中的参数列表具有…运算符在里面,基本上意思是“…其他的都应该放到$strings中。您可以向该函数传递2个或更多参数,第二个和后续的参数将被添加到$strings数组中,随时可以使用。
这就是所谓的“splat”运算符。基本上这句话的意思是“任意数量的参数”;由PHP 5.6引入
详情请看这里。
省略号有两种用法(…)PHP令牌—可以将它们看作打包数组和解包数组。这两个目的都适用于函数参数。
Pack
When defining a function, if you need a dynamic number of variables provided to the function (i.e., you don't know how many arguments will be provided to that function when called in your code), prefix the ellipsis (...) token to a variable name—e.g., ...$numbers—to capture all (remaining) arguments provided to that function into an array assigned to the named variable—in this case $numbers—that is accessible inside the function block. The number of arguments captured by prefixing ellipsis (...) can be zero or more.
例如:
// function definition
function sum (...$numbers) { // use ellipsis token when defining function
$acc = 0;
foreach ($numbers as $nn) {
$acc += $nn;
}
return $acc;
}
// call the function
echo sum(1, 2, 3, 4); // provide any number of arguments
> 10
// and again...
echo sum(1, 2, 3, 4, 5);
> 15
// and again...
echo sum();
> 0
当在函数实例化中使用打包时,在变量名前加上省略号(…)会捕获所有剩余的参数,也就是说,你仍然可以有任何数字为0或更多的初始固定(位置)参数:
function sum ($first, $second, ...$remaining_numbers) {
$acc = $first + $second;
foreach ($remaining_numbers as $nn) {
$acc += $nn;
}
return $acc;
}
// call the function
echo sum(1, 2); // provide at least two arguments
> 3
// and again...
echo sum(1, 2, 3, 4); // first two are assigned to fixed arguments, the rest get "packed"
> 10
...带前缀的省略号变量捕获其余所有内容。因此,它必须是最后一个函数参数。
解压缩
或者,在调用函数时,如果您提供给该函数的参数之前被组合到一个数组中,则使用省略号(…)令牌前缀变量“inline”将该数组变量转换为提供给该函数的单独参数。当任意数量的函数实参被替换为带有省略号前缀的变量时,每个数组元素被赋值给函数定义中命名的函数实参变量。
例如:
function add ($aa, $bb, $cc) {
return $aa + $bb + $cc;
}
$arr = [1, 2, 3];
echo add(...$arr); // use ellipsis token when calling function
> 6
$first = 1;
$arr = [2, 3];
echo add($first, ...$arr); // used with positional arguments
> 6
$first = 1;
$arr = [2, 3, 4, 5]; // array can be "oversized"
echo add($first, ...$arr); // remaining elements are ignored
> 6
在使用数组函数操作数组或变量时,解包特别有用。
例如,解包array_slice的结果:
function echoTwo ($one, $two) {
echo "$one\n$two";
}
$steaks = array('ribeye', 'kc strip', 't-bone', 'sirloin', 'chuck');
// array_slice returns an array, but ellipsis unpacks it into function arguments
echoTwo(...array_slice($steaks, -2)); // return last two elements in array
> sirloin
> chuck
要使用此特性,只需警告PHP需要使用…将数组解压缩为变量。操作符。更多细节请看这里,一个简单的例子可以是这样的:
$email[] = "Hi there";
$email[] = "Thanks for registering, hope you like it";
mail("someone@example.com", ...$email);
在PHP 7.4中,省略号也是展开操作符:
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
来源:https://wiki.php.net/rfc/spread_operator_for_array