即将推出的 PHP8 的展开运算符和它之前存在的紧凑函数有什么区别?
What's the difference between upcoming PHP8's spread operator and it's pre-existing compact function?
即将推出的 PHP8 的展开运算符和它之前存在的 compact()
函数有什么区别?
$data = new CustomerData(...$input);
紧凑运算符和展开运算符之间的区别
紧凑PHP函数:
compact — Create array containing variables and their values
<?php
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array("city", "state");
$result = compact("event", $location_vars);
print_r($result);
?>
哪个输出:
Array
(
[event] => SIGGRAPH
[city] => San Francisco
[state] => CA
)
数组展开:
while array spreading merge two or more array without adding a key to his value (associative array):
$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
$arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111]
$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]
数组展开也可用于填充方法调用的参数,假设您的 class:
具有此构造函数
class CustomerData
{
public function __construct(
public string $name,
public string $email,
public int $age,
) {}
}
并使用数组展开创建CustomerData
对象:
$input = [
'age' => 25,
'name' => 'Brent',
'email' => 'brent@stitcher.io',
];
$data = new CustomerData(...$input);
与以下行为相同:
$input = [
'age' => 25,
'name' => 'Brent',
'email' => 'brent@stitcher.io',
];
$data = new CustomerData($input['age'], $input['name'], $input['email']);
PHP has already supported argument unpacking (AKA spread operator) since 5.6. This RFC proposes to bring this feature to array expression.
自 PHP 7.4.
以来还实现了数组表达式中的展开运算符
来源:
PHP: Spread Operator in Array Expression
即将到来 PHP 8: 命名参数
Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent, and allows skipping default values arbitrarily
class CustomerData
{
public function __construct(
public string $name,
public string $email,
public int $age,
) {}
}
$data = new CustomerData(
name: $input['name'],
email: $input['email'],
age: $input['age'],
);
即将推出的 PHP8 的展开运算符和它之前存在的 compact()
函数有什么区别?
$data = new CustomerData(...$input);
紧凑运算符和展开运算符之间的区别
紧凑PHP函数:
compact — Create array containing variables and their values
<?php
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array("city", "state");
$result = compact("event", $location_vars);
print_r($result);
?>
哪个输出:
Array
(
[event] => SIGGRAPH
[city] => San Francisco
[state] => CA
)
数组展开:
while array spreading merge two or more array without adding a key to his value (associative array):
$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
$arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111]
$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]
数组展开也可用于填充方法调用的参数,假设您的 class:
具有此构造函数class CustomerData
{
public function __construct(
public string $name,
public string $email,
public int $age,
) {}
}
并使用数组展开创建CustomerData
对象:
$input = [
'age' => 25,
'name' => 'Brent',
'email' => 'brent@stitcher.io',
];
$data = new CustomerData(...$input);
与以下行为相同:
$input = [
'age' => 25,
'name' => 'Brent',
'email' => 'brent@stitcher.io',
];
$data = new CustomerData($input['age'], $input['name'], $input['email']);
PHP has already supported argument unpacking (AKA spread operator) since 5.6. This RFC proposes to bring this feature to array expression.
自 PHP 7.4.
以来还实现了数组表达式中的展开运算符来源:
PHP: Spread Operator in Array Expression
即将到来 PHP 8: 命名参数
Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent, and allows skipping default values arbitrarily
class CustomerData
{
public function __construct(
public string $name,
public string $email,
public int $age,
) {}
}
$data = new CustomerData(
name: $input['name'],
email: $input['email'],
age: $input['age'],
);