Php 简明教程
PHP - Named Arguments
PHP 的 8.0 版本引入了命名参数功能。它是对调用函数时传递位置参数现有机制的扩展。
The feature of Named Arguments has been introduced in PHP with the version 8.0. It is an extension of the existing mechanism of passing positional arguments to a function while calling.
默认情况下,传递的参数值会复制到相同位置上的对应形式参数。PHP 中的此命名参数功能使得可以根据参数名称传递值,而不是根据位置传递。
By default, values of passed arguments are copied to the corresponding formal arguments at the same position. This feature of named arguments in PHP makes it possible to pass the value based on the parameter name instead of the position.
如果我们定义了一个函数,如下所示 −
If we have a function defined as follows −
function myfunction($x, $y) {
statement1;
statement2;
. . .
}
并且它被调用为 −
and it is called as −
myfunction(10, 20);
在此情况下,这些值按声明顺序传递给变量“x”和“y”。也就是说,第一个值传递给第一个参数,第二个值传递给第二个参数,以此类推。变量“x”和“y”是位置自变量。
In this case, the values are passed to the variables "x" and "y" in the order of declaration. It means, the first value to the first argument, second value to second argument and so on. The variables "x" and "y" are positional arguments.
要通过已命名自变量传递值,请指定要将参数传递给其值的变量名。参数的名称是形式参数的名称,但没有“$”符号。要传递的值放在冒号“:”符号的前面。
To pass the values by named arguments, specify the parameter name to which argument the value is to be passed. The name of the parameter is the name of formal argument without the "$" symbol. The value to be passed is put in front of the ":" symbol.
myfunction(x:10, y:20);
Example
这是一段展示了如何在 PHP 中使用 named arguments 的代码 −
Here is the code that demonstrates how you can use named arguments in PHP −
<?php
function myfunction($x, $y) {
echo "x = $x y = $y";
}
myfunction(x:10, y:20);
?>
它将生成以下 output −
It will produce the following output −
x = 10 y = 20
通过使用 named arguments ,可以按照任何顺序传递值,并且不一定要与参数在函数定义中声明的顺序相同。我们可以按如下所示调用 myfunction() ,它将生成相同的结果。
Using named arguments makes it possible to pass the values in any order, and not necessarily in the same order in which the arguments are declared in the function definition. We can call myfunction() as shown below and it will produce the same result.
myfunction(y:20, x:10);
借助此特性,自变量将独立于顺序并且可以自文档化。它还可以跳过带有默认值的自变量。
With this feature, the arguments become order-independent and self-documenting. It also makes it possible to skip the arguments with default values arbitrarily.
Combining Named Arguments with Positional Arguments
可以将已命名自变量与位置自变量结合使用,条件是已命名自变量必须位于位置自变量之后。
Named arguments can be combined with positional arguments, with the condition that, the named arguments must come after the positional arguments.
Example
<?php
function myfunction($x, $y, $z) {
echo "x = $x y = $y z = $z";
}
myfunction(10, z:20, y:30);
?>
它将生成以下 output −
It will produce the following output −
x = 10 y = 30 z = 20
但是,如果你尝试将 $z 作为位置自变量处理,
However, if you try to treat $z as a positional argument,
myfunction(x:10, y:20, 30);
在此情况下,PHP 将遇到以下 error −
In this case, PHP will encounter the following error −
PHP Fatal error: Cannot use positional argument after
named argument in hello.php on line 7
Passing Named Arguments from an Array
PHP 8.1.0 还引入了另一项特性,该特性允许在取消参数打包后使用已命名自变量。可以使用数组中的“…”(三个点),而不是单独向每个参数提供值,将数组中的值解包到对应的参数中。
PHP 8.1.0 also introduced another feature that allows using named argument after unpacking the arguments. Instead of providing values to each argument individually, the values in an array an be unpacked into the corresponding arguments, using "…" (three dots) before the array.
Example
<?php
function myfunction($x, $y, $z=30) {
echo "x = $x y = $y z = $z";
}
myfunction(...[10, 20], z:30);
?>
它将生成以下 output −
It will produce the following output −
x = 10 y = 20 z = 30
请注意,多次传递同一个参数将导致异常,如下所示 −
Note that passing the same parameter multiple times results in an exception as follows −
myfunction(x:10, z:20, x:20);
Error −
Error −
PHP Fatal error: Uncaught Error: Named parameter $x
overwrites previous argument in hello.php:7