- 实验环境:https://onlinephp.io/
- Polyfill https://github.com/symfony/polyfill/tree/main/src
PHP8.0 to PHP8.1
https://www.php.net/manual/en/migration81.php https://php.watch/versions/8.1 brings major new features such as Enums, Fibers, never return type, Intersection Types, readonly properties, and more, while ironing out some of its undesired legacy features by deprecating them.
New Features 8.1
代码语言:javascript复制// 八进制
var_dump(0o14);
// int(12)
代码语言:javascript复制// 可解包字符串为 key 的数组
$arr1 = [1, 'a' => 'b'];
var_dump([...$arr1, 'c' => 'd']);
代码语言:javascript复制// 参数解包后的命名参数
function foo($a, $b, $c) {
var_dump($a, $b, $c);
}
foo(...[1,2], c: 3);
代码语言:javascript复制// Enumerations 枚举
// https://www.php.net/manual/en/language.enumerations.examples.php
代码语言:javascript复制// Fibers 纤程
// https://www.php.net/manual/en/language.fibers.php
代码语言:javascript复制// Intersection Types 交叉类型
// 由类型 T、U 和 V 组成的交集类型将写为 T&U&V
代码语言:javascript复制// Never type
代码语言:javascript复制// new in Initializers
// 现在可以使用 new ClassName() 表达式作为参数、静态变量、全局常量初始值设定项和属性参数的默认值
// 现在也可以将对象传递给 define()
代码语言:javascript复制// Readonly properties
// Support for readonly has been added.
New Functions 8.1
代码语言:javascript复制// 如果数组的键由从 0 到 count($array)-1 的连续数字组成,则数组被认为是 list
array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true
// The array does not start at 0
array_is_list([1 => 'apple', 'orange']); // false
– EOF –
- # php