WARNING
PHP 8.5 仍在开发中;首个正式版本计划于 2025 年 11 月 20 日发布。如果你想尝试,nightly tarballs 和 Docker 镜像(php:8.5.0-dev)已经可用。
领域 | 特性 |
---|---|
语言语法 | 管道操作符 |> |
标准库 | array_first() , array_last() , 新的 IntlListFormatter , curl_multi_get_handles() , locale_is_right_to_left() |
属性 | #[\NoDiscard] + (void) 强制转换 |
核心调整 | Directory 成为不透明/最终类 |
性能/IO | 原生 Zstandard 和 Brotli 压缩 |
|>
2025 年 2 月批准,管道将左侧值传递给右侧的可调用对象,并从左到右求值。
$result = "Hello World"
|> htmlentities(...)
|> str_split(...)
|> fn($x) => array_map(strtoupper(...), $x)
|> fn($x) => array_filter($x, fn ($v) => $v !== 'O');
var_dump($result); // ['H','E','L','L','W','R','D']
record
关键字(RFC 正在讨论中,但趋势积极)让你可以用几乎零样板代码声明按值传递的不可变类型。
record Point(int $x, int $y);
$origin = &Point(0, 0); // 前导的“&”表示记录实例化
echo $origin->x; // 0
final
很快你就可以将提升的属性标记为 final
,保证它们在构造后永不改变。投票已经压倒性地支持。
class User
{
public function __construct(
final string $id,
public string $name,
) {}
}
静态闭包现在可以在 const
、属性参数和默认属性/参数值中使用 — 对于默认回调非常方便。已为 8.5 实现。
const DEFAULT_FILTER = static function (mixed $v): bool {
return !empty($v);
};
#[\NoDiscard]
+ (void)
强制转换 标记必须消耗其返回值的函数;如果你真的想忽略它,可以将调用强制转换为 (void)
以消除警告。
#[\NoDiscard('You probably meant to use this')]
function compute(): array
{
return [...];
}
(void) compute(); // 显式丢弃 – 无警告
✨ 新的 API | 为什么你会关心 |
---|---|
array_first() / array_last() | 在 O(1) 时间内获取第一个或最后一个元素,而不触及内部指针。 |
curl_multi_get_handles() | 枚举 multi-curl 资源中的所有句柄。 |
IntlListFormatter | 支持 i18n 的人性化列表(例如“apples, pears and peaches”)。 |
locale_is_right_to_left() | 无需 ICU 查找的快速 RTL 检查。 |
php --ini=diff | CLI 标志,仅打印你调整过的 INI 条目。 |
PHP_BUILD_DATE | 精确的构建时间戳嵌入到二进制文件中。 |
$items = ['🍎','🍐','🍑'];
echo array_first($items); // 🍎
echo array_last($items); // 🍑
Directory
现在是不透明的 你不能再 new Directory()
或克隆/序列化它;将其视为真正的资源对象。
$dir = dir('.');
clone $dir; // 致命错误
new Directory(); // 致命错误
Zstandard
和 Brotli
加入 zlib
,为你提供快速、原生的 Web 压缩。
$zip = zstd_compress($data);
$plain = brotli_uncompress($zip);
所有 MHASH_*
常量都将被弃用。
随着 8.5 的稳定,预计会有一轮新的小边缘案例清理。
PHP 8.5 不是一个革命性的版本,但它带来了大量的人体工程学改进: