/

PHP Migrating to 8.1

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

// 八进制
var_dump(0o14);
// int(12)
// 可解包字符串为 key 的数组
$arr1 = [1, 'a' => 'b'];
var_dump([...$arr1, 'c' => 'd']);
// 参数解包后的命名参数
function foo($a, $b, $c) {
var_dump($a, $b, $c);
}
foo(...[1,2], c: 3);
// Enumerations 枚举
// https://www.php.net/manual/en/language.enumerations.examples.php
// Fibers 纤程
// https://www.php.net/manual/en/language.fibers.php
// Intersection Types 交叉类型
// 由类型 T、U 和 V 组成的交集类型将写为 T&U&V
// Never type
// new in Initializers
// 现在可以使用 new ClassName() 表达式作为参数、静态变量、全局常量初始值设定项和属性参数的默认值
// 现在也可以将对象传递给 define()
// Readonly properties
// Support for readonly has been added.

New Functions 8.1

// 如果数组的键由从 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 –