搜索
  • 登录
  • 注册
密码重置成功

关注您感兴趣的项目并了解有关它们的最新消息

更多变更日志

更智能,更快速,为未来而生。

PHP 8.5 是 PHP 语言的重大更新, 包含新功能,包括 URI 扩展、管道操作符,以及克隆时修改属性的支持。

立即升级到 PHP 8.5!

URI 扩展

新的始终可用的 URI 扩展提供 API,可根据 RFC 3986 和 WHATWG URL 标准安全地解析和修改 URI 与 URL。

由 uriparser (RFC 3986) 和 Lexbor (WHATWG URL) 库提供支持。

在 PHP Foundation 的博客中了解该功能的背景故事。

PHP < 8.5
$components = parse_url ( 'https://php.net/releases/8.5/en.php' );

var_dump ( $components [ 'host' ]);
// string(7) "php.net"
PHP 8.5
use Uri\Rfc3986\Uri ;

$uri = new Uri ( 'https://php.net/releases/8.5/en.php' );

var_dump ( $uri -> getHost ());
// string(7) "php.net"

管道操作符

管道操作符允许将函数调用串联在一起,而无需处理中间变量。这可以将许多“嵌套调用”替换为可顺序读取的链,而不是从内向外。

在 PHP Foundation 的博客中了解该功能的背景故事。

PHP < 8.5
$title = ' PHP 8.5 Released ' ;

$slug = strtolower (
str_replace ( '.' , '' ,
str_replace ( ' ' , '-' ,
trim ( $title )
)
)
);

var_dump ( $slug );
// string(15) "php-85-released"
PHP 8.5
$title = ' PHP 8.5 Released ' ;

$slug = $title
|> trim (...)
|> (fn( $str ) => str_replace ( ' ' , '-' , $str ))
|> (fn( $str ) => str_replace ( '.' , '' , $str ))
|> strtolower (...);

var_dump ( $slug );
// string(15) "php-85-released"

带属性克隆

现在可以在对象克隆时,通过向 clone() 函数传递关联数组来更新属性。这使只读类的 "with-er" 模式得到直接支持。

PHP < 8.5
readonly class Color
{
public function __construct (
public int $red ,
public int $green ,
public int $blue ,
public int $alpha = 255 ,
) {}

public function withAlpha ( int $alpha ): self
{
$values = get_object_vars ( $this );
$values [ 'alpha' ] = $alpha

; return new self (... $values );
}
}

$blue = new Color ( 79 , 91 , 147 );
$transparentBlue = $blue -> withAlpha ( 128 );
PHP 8.5
readonly class Color
{
public function __construct (
public int $red ,
public int $green ,
public int $blue ,
public int $alpha = 255 ,
) {}

public function withAlpha ( int $alpha ): self
{
return clone( $this , [
'alpha' => $alpha ,
]);
}
}

$blue = new Color ( 79 , 91 , 147 );
$transparentBlue = $blue -> withAlpha ( 128 );

#[\NoDiscard] 属性

通过在函数上添加 #[\NoDiscard] 属性,PHP 会检查返回值是否被使用,如果未使用将发出警告。这有助于提高 API 的安全性,避免因忘记使用返回值而出现问题。

关联的 (void) 类型转换可以用来表示该值是故意未使用的。

PHP < 8.5
function getPhpVersion (): string
{
return 'PHP 8.4' ;
}

getPhpVersion (); // No warning
PHP 8.5
#[ \NoDiscard ]
function getPhpVersion (): string
{
return 'PHP 8.5' ;
}

getPhpVersion ();
// Warning: The return value of function getPhpVersion() should
// either be used or intentionally ignored by casting it as (void)

常量表达式中的闭包和一等可调用

现在可以在常量表达式中使用静态闭包和一等可调用。这包括属性参数、属性和参数的默认值,以及常量。

PHP < 8.5
final class PostsController
{
#[ AccessControl (
new Expression ( 'request.user === post.getAuthor()' ),
)]
public function update (
Request $request ,
Post $post ,
): Response {
// ...
}
}
PHP 8.5
final class PostsController
{
#[ AccessControl (static function (
Request $request ,
Post $post ,
): bool {
return $request -> user === $post -> getAuthor ();
})]
public function update (
Request $request ,
Post $post ,
): Response {
// ...
}
}

持久化 cURL 共享句柄

与 curl_share_init() 不同,由 curl_share_init_persistent() 创建的句柄在 PHP 请求结束时不会被销毁。如果找到具有相同共享选项的持久化共享句柄,它将被重用,从而避免每次初始化 cURL 句柄的开销。

PHP < 8.5
$sh = curl_share_init ();
curl_share_setopt ( $sh , CURLSHOPT_SHARE , CURL_LOCK_DATA_DNS );
curl_share_setopt ( $sh , CURLSHOPT_SHARE , CURL_LOCK_DATA_CONNECT );

$ch = curl_init ( 'https://php.net/' );

curl_setopt ( $ch , CURLOPT_SHARE , $sh );

curl_exec ( $ch );
PHP 8.5
$sh = curl_share_init_persistent ([
CURL_LOCK_DATA_DNS ,
CURL_LOCK_DATA_CONNECT ,
]);

$ch = curl_init ( 'https://php.net/' );
curl_setopt ( $ch , CURLOPT_SHARE , $sh );

// This may now reuse the connection from an earlier SAPI request
curl_exec ( $ch );

array_first() 和 array_last() 函数

array_first() 和 array_last() 函数分别返回数组的第一个或最后一个值。如果数组为空,则返回 null(方便与 ?? 操作符组合使用)。

PHP < 8.5
$lastEvent = $events === []
? null
: $events [ array_key_last ( $events )];
PHP 8.5
$lastEvent = array_last ( $events );

其他功能和改进

  • 致命错误(例如超过最大执行时间)现在包含回溯信息。
  • 属性现在可以应用于常量。
  • #[\Override] 属性现在可以应用于属性。
  • #[\Deprecated] 属性可以用于 trait 和常量。
  • 静态属性现在支持非对称可见性。
  • 可以使用构造函数属性提升将属性标记为 final。
  • 新增 Closure::getCurrent() 方法,以简化匿名函数中的递归。
  • setcookie() 和 setrawcookie() 函数现在支持 "partitioned" 键。
  • 新增 get_error_handler() 和 get_exception_handler() 函数。
  • 新增 Dom\Element::getElementsByClassName() 和 Dom\Element::insertAdjacentHTML() 方法。
  • 新增 grapheme_levenshtein() 函数。
  • 新增 #[\DelayedTargetValidation] 属性,可用于抑制核心和扩展属性在无效目标上使用时产生的编译时错误。

弃用和向后兼容性中断

  • 反引号操作符作为 shell_exec() 的别名已被弃用。
  • 非标准类型转换名称 (boolean)、(integer)、(double) 和 (binary) 已弃用。请分别使用 (bool)、(int)、(float) 和 (string)。
  • INI 设置 disable_classes 已被移除,因为它会破坏引擎的各种假设。
  • 使用分号而非冒号结束 case 语句已被弃用。
  • 在数组偏移或调用 array_key_exists() 时使用 null 已被弃用,请使用空字符串代替。
  • 在 class_alias() 中不再可能使用 "array" 和 "callable" 作为类别名。
  • __sleep() 和 __wakeup() 魔术方法已被软弃用,请使用 __serialize() 和 __unserialize()。
  • 将 NAN 转换为其他类型时会发出警告。
  • 使用 [] 或 list() 解构非数组值(除 null 外)现在会发出警告。
  • 将浮点数(或看起来像浮点数的字符串)转换为 int 时,如果无法表示,将发出警告。
更好的性能、更好的语法、改进的类型安全性。 立即升级到 PHP 8.5!

有关PHP 8.5的源下载,请访问 下载 页。 Windows二进制文件可以在 Windows的PHP 地点。更改列表记录在 变更日志.

迁移指南 可在PHP手册中使用。请咨询以获取新功能和向后不兼容的更改的详细列表。