php 8 jit,PHP 8.0重大版本更新正式发布:支持JIT编译器,性能提升高达3倍

原标题:PHP 8.0重大版本更新正式发布:支持JIT编译器,性能提升高达3倍

d5654c17748a611e507a7a2bf539d5f9.png

来源 | PHP 官网

编译整理 | 王强、蔡芳芳

来源公众号丨前端之巅(ID:frontshow)

https://mp.weixin.qq.com/s/SfHs0HkUM74-MsvfLO3RdQ

美国时间 11 月 26 日,PHP 团队宣布 PHP 8.0 正式 GA。PHP 8.0 是 PHP 语言的最新主要版本,带来了许多新特性和优化,包括命名参数(named arguments)、联合类型(union types)、属性(attributes)、构造器属性提升(constructor property promotion)、Match 表达式、nullsafe 运算符、JIT,以及针对类型系统、错误处理和一致性的诸多改进。

15083dcce6da14bf106f8ee0b6ad34c1.png

PHP 8.0.0 下载地址:

https://www.php.net/downloads

下文将对新版本的重要亮点做简单介绍:

命名参数

https://wiki.php.net/rfc/named_params

PHP 7

htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

PHP 8

htmlspecialchars($string, double_encode: false);

仅指定必需参数,跳过可选参数。

参数与顺序无关,且是自描述的。

属性

现在,开发者可以使用基于 PHP 原生语法的结构化元数据来代替 PHPDoc 注解。

https://wiki.php.net/rfc/attributes_v2

PHP 7

classPostsController

{

/**

* @Route("/api/posts/{id}", methods={"GET"})

*/

publicfunctionget($id){ /* ... */}

}

PHP 8

classPostsController

{

#[Route("/api/posts/{id}", methods: ["GET"])]

publicfunctionget($id){ /* ... */}

}

构造器属性提升

新版本定义和初始化属性所用的样板代码更少。

https://wiki.php.net/rfc/constructor_promotion

PHP 7

classPoint{

publicfloat $x;

publicfloat $y;

publicfloat $z;

publicfunction__construct(

float $x = 0.0,

float $y = 0.0,

float $z = 0.0,

) {

$this->x = $x;

$this->y = $y;

$this->z = $z;

}

}

PHP 8

classPoint{

publicfunction __construct(

publicfloat$x = 0.0,

publicfloat$y = 0.0,

publicfloat$z = 0.0,

) {}

}

联合类型(Union Types)

Union Types 支持接收多个不同类型的值,而不是单个类型。目前 PHP 已经支持两种特殊的联合类型:

Type 或 null,使用特殊?Type 语法。

array 或 Traversable,使用特殊 iterable 类型。

对于类型组合,可以使用在运行时经过验证的原生联合类型声明来代替 PHPDoc 注解。

https://wiki.php.net/rfc/union_types_v2

支持联合类型之后,将会允许将更多类型信息从 phpdoc 迁移至函数签名。可以说,泛型之后,联合类型是目前类型声明系统中最大的突破口。

PHP 7

classNumber{

/** @varint|float */

private$number;

/**

* @paramfloat|int $number

*/

publicfunction__construct($number){

$this->number = $number;

}

}

newNumber( 'NaN'); // Ok

PHP 8

classNumber{

publicfunction__construct(

privateint|float $ number

) {}

}

newNumber( 'NaN'); // TypeError

Match 表达式

新的 match 很像 switch,并具有以下特性:

Match 是一个表达式,表示其结果可以存储在变量中或返回。

Match 分支仅支持单行表达式,不需要 break; 语句。

Match 执行严格比较。

https://wiki.php.net/rfc/match_expression_v2

PHP 7

switch( 8.0) {

case'8.0':

$result = "Oh no!";

break;

case8.0:

$result = "This is what I expected";

break;

}

echo$result;

//> Oh no!

PHP 8

echomatch ( 8.0) {

'8.0'=> "Oh no!",

8.0=> "This is what I expected",

};

//> This is what I expected

Nullsafe 运算符

现在,开发者可以使用带有新的 nullsafe 运算符的调用链来代替 null check。当对链中一个元素的求值失败时,整个链的执行将中止,并且整个链的求值为 null。

https://wiki.php.net/rfc/nullsafe_operator

PHP 7

$country = null;

if($session !== null) {

$user = $session->user;

if($user !== null) {

$address = $user->getAddress;

if($address !== null) {

$country = $address->country;

}

}

}

PHP 8

$country = $session?->user?->getAddress?->country;

字符串与数字的判断更合理

使用 == 和其他非严格比较运算符对字符串和数字之间做比较时,原本的做法是将字符串强制转换为数字,然后对整数或浮点数进行比较。这会导致许多令人惊讶的比较结果,其中最值得注意的是 0 == "foobar" 返回 true。

在新版本中,仅在字符串实际为数字时才使用数字比较,否则将数字转换为字符串,并执行字符串比较。

https://wiki.php.net/rfc/string_to_number_comparison

PHP 7

0== 'foobar'// true

PHP 8

0== 'foobar'// false

内部函数的类型错误一致

在新版本中,如果参数验证失败,大多数内部函数将抛出 Error 异常。

https://wiki.php.net/rfc/consistent_type_errors

PHP 7

strlen( []); // Warning: strlen expects parameter 1 to be string, array given

array_chunk( [], -1); // Warning: array_chunk: Size parameter expected to be greater than 0

PHP 8

strlen( []); // TypeError: strlen: Argument #1 ($str) must be of type string, array given

array_chunk( [], -1); // ValueError: array_chunk: Argument #2 ($length) must be greater than 0

JIT 编译

PHP 8 引入了两个 JIT 编译引擎。Tracing JIT 的表现最出色,它在综合基准测试中的性能提高到大约 3 倍,在某些特定的传统应用程序中提高到 1.5–2 倍。典型的应用程序性能与 PHP 7.4 相当。

JIT 对 PHP 8 性能的贡献

56a1fafae94a2e138ca865b6947548c2.png

类型系统和错误处理方面的改进

对算术 / 按位运算符进行更严格的类型检查(https://wiki.php.net/rfc/arithmetic_operator_type_checks)

抽象特征方法验证(https://wiki.php.net/rfc/abstract_trait_method_validation)

魔术方法的正确签名(https://wiki.php.net/rfc/magic-methods-signature)

重分类引擎警告(https://wiki.php.net/rfc/engine_warnings)

不兼容方法签名的致命错误(https://wiki.php.net/rfc/lsp_errors)

@运算符不再使致命错误静默。

用私有方法继承(https://wiki.php.net/rfc/inheritance_private_methods)

混合类型(https://wiki.php.net/rfc/mixed_type_v2)

静态返回类型(https://wiki.php.net/rfc/static_return_type)

不透明的对象代替 Curl、Gd、Sockets、OpenSSL、XMLWriter 和 XML 扩展的资源

其他语法调整和改进

在参数列表(https://wiki.php.net/rfc/trailing_comma_in_parameter_list)和使用闭包的列表(https://wiki.php.net/rfc/trailing_comma_in_closure_use_list)中允许结尾逗号

non-capturing 捕获(https://wiki.php.net/rfc/non-capturing_catches)

变量语法调整(https://wiki.php.net/rfc/variable_syntax_tweaks)

将命名空间名称视为单个令牌(https://wiki.php.net/rfc/namespaced_names_as_token)

Throw 现在是表达式(https://wiki.php.net/rfc/throw_expression)

在对象上允许::class(https://wiki.php.net/rfc/class_name_literal_on_object)

新的类、接口和函数

Weak Map 类(https://wiki.php.net/rfc/weak_maps)

Stringable 接口(https://wiki.php.net/rfc/stringable)

str_contains、str_starts_with、str_ends_with(https://wiki.php.net/rfc/str_contains)

fdiv(https://github.com/php/php-src/pull/4769)

get_debug_type(https://wiki.php.net/rfc/get_debug_type)

get_resource_id(https://github.com/php/php-src/pull/54270

token_get_all 对象实现(https://wiki.php.net/rfc/token_as_object)

下载

要下载 PHP 8 的源代码,请访问下载页面:

https://www.php.net/downloads

Windows 二进制文件位于 Windows 版 PHP 网站:

http://windows.php.net/download

更改列表位于 ChangeLog:

http://www.php.net/ChangeLog-8.php

PHP 手册中提供了迁移指南。请查阅它以获取新特性细节和向后不兼容更改的详细列表。

https://www.php.net/manual/en/migration80.php

延伸阅读

https://www.php.net/releases/8.0/en.php返回搜狐,查看更多

责任编辑:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值