/

PHP Migrating 8.0 to 8.1

PHP 8.1 contains many new features, including enums, readonly properties, first-class callable syntax, fibers, intersection types, performance improvements and more.

New Features 8.1

// Add native support for enumerations using enum keyword
// https://www.php.net/manual/en/language.enumerations.examples.php
enum Status: string {
case Draft = 'draft';
case Published = 'published';
case Archived = 'archived';
}
class BlogPost {
public function __construct(
public Status $status = Status::Draft
) {}
}
// Readonly properties
// Properties can be marked as immutable after initialization
class User {
public readonly string $uuid;
public function __construct(string $uuid) {
$this->uuid = $uuid;
}
}
// Fibers, new concurrency primitive for managing green threads
// https://www.php.net/manual/en/language.fibers.php
$fiber = new Fiber(function(): void {
echo "Starting fiber\n";
Fiber::suspend();
echo "Resuming fiber\n";
});
$fiber->start();
echo "...\n";
$fiber->resume();
// Starting fiber
// ...
// Resuming fiber
// Intersection Types, combine multiple type constraints
// 交叉类型
function countItems(Iterator&Countable $items): int {
return count($items);
}
// Never Return Type
// Indicates function never returns
function redirect(string $url): never {
header("Location: $url");
exit();
}
// First-Class Callable Syntax
// Replaces Closure::fromCallable() with cleaner syntax
$strlen = strlen(...);
echo $strlen('hello'); // 5
// same as
$fn = Closure::fromCallable('strlen');
echo $fn('hello'); // 5

$words = ['apple', 'banana'];
var_dump(array_map(strlen(...), $words)); // [5, 6]

class Calculator {
public function add(int $a, int $b): int {
return $a + $b;
}
public function __invoke(int $x) {
return $x * 2;
}
}
$calc = new Calculator();
$addCallable = $calc->add(...);
echo $addCallable(3, 5); // 8
$invocable = $calc(...);
echo $invocable(10); // 20
// New Array Features
// String key unpacking
$arr1 = [1, 'a' => 'b'];
var_dump([...$arr1, 'c' => 'd']);
// [1,'a' => 'b', 'c' => 'd']

// 参数解包后的命名参数
function foo($a, $b, $c) {
var_dump($a, $b, $c);
}
foo(...[1,2], c: 3);
// File System Improvements
$file = fopen('data.txt', 'w');
fwrite($file, 'content');
fsync($file); // Force write to disk
fclose($file);
// Objects in default parameters
// 现在可以使用 new ClassName() 表达式作为参数、静态变量、全局常量初始值设定项和属性参数的默认值
// 现在也可以将对象传递给 define()
class Logger {
public function __construct(
private Formatter $fmt = new DefaultFormatter()
) {}
}
// 八进制
var_dump(0o14);
// int(12)

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

Backward Incompatible Changes 8.1

// Usage of static Variables in Inherited Methods ¶
class A {
public static function counter() {
static $i = 0;
return ++$i;
}
}
class B extends A {}
var_dump(A::counter()); // int(1)
var_dump(A::counter()); // int(2)
var_dump(B::counter());
// PHP 8.0 int(1)
// PHP 8.1+ int(3)

Deprecated 8.1

// Implicit incompatible float to int conversions
$arr = [];
$arr[3.14] = 'value'; // Deprecated(键值3.14转为3)
$arr[(int)3.14] = 'value'; // 显式转换避免警告

– EOF –