php 8 jit,PHP8正式版发布,带来了注解和JIT

format,png

PHP8 正式版已经发布,它引入了一些重大变更,以及许多新特性和性能优化,包括命名参数、联合类型、注解、Constructor Property Promotion、match 表达式、nullsafe 运算符、JIT,以及对类型系统、错误处理和一致性的改进。

之前的PHPCon上听过Nikic的一些分享,感兴趣的小伙伴可以查看Nikic的PPT

1c0c828e05a320c4714719549eec0835.png

在 PHP官网 也提到了一些新特性和功能说明,我们来看一看

命名参数

//PHP7

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

//PHP8

htmlspecialchars($string, double_encode: false);

1. 只指定必需的参数,跳过可选参数。

2. 参数是独立于顺序和自我记录的。

属性

Attributes ,也就是我们常说的注解,而且语法不会影响低版本,因为 # 在PHP中是注释符号

//PHP7

class PostsController

{

/**

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

*/

public function get($id) { /* ... */ }

}

//PHP8

class PostsController

{

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

public function get($id) { /* ... */ }

}

构造函数属性提升

Constructor property promotion ,让我们在定义构造函数的同时定义属性,减少代码量,提升编码效率

//PHP7

class Point {

public float $x;

public float $y;

public float $z;

public function __construct(

float $x = 0.0,

float $y = 0.0,

float $z = 0.0,

) {

$this->x = $x;

$this->y = $y;

$this->z = $z;

}

}

//PHP8

class Point {

public function __construct(

public float $x = 0.0,

public float $y = 0.0,

public float $z = 0.0,

) {}

}

联合类型

Union types ,支持声明不止一个类型

//PHP7

class Number {

/** @var int|float */

private $number;

/**

* @param float|int $number

*/

public function __construct($number) {

$this->number = $number;

}

}

new Number('NaN'); // Ok

//PHP8

class Number {

public function __construct(

private int|float $number

) {}

}

new Number('NaN'); // TypeError

匹配表达式

新匹配与switch类似,具有以下功能:

1. Match是一个表达式,意味着它的结果可以存储在变量中或返回。

2. 匹配分支只支持单行表达式,不需要break;语句。

3. Match进行严格的比较。

//PHP7

switch (8.0) {

case '8.0':

$result = "Oh no!";

break;

case 8.0:

$result = "This is what I expected";

break;

}

echo $result;

//> Oh no!

//PHP8

echo match (8.0) {

'8.0' => "Oh no!",

8.0 => "This is what I expected",

};

//> This is what I expected

NULL 安全运算符

Nullsafe operator ,现在我们可以使用新的nullsafe操作符来代替空检查条件。当对链中的一个元素求值失败时,整个链的执行将中止,整个链的计算结果为null

这个特性确实挺不错的,减少了不少代码量和逻辑代码

//PHP7

$country = null;

if ($session !== null) {

$user = $session->user;

if ($user !== null) {

$address = $user->getAddress();

if ($address !== null) {

$country = $address->country;

}

}

}

//PHP8

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

更合理的字符串与数字比较

Saner string to number comparisons ,当与数字字符串进行比较时,PHP8使用数字比较。否则,它将数字转换为字符串并使用字符串比较

//PHP7

0 == 'foobar' // true

//PHP8

0 == 'foobar' // false

内部函数的一致类型错误

//PHP7

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

//PHP8

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

PHP8最值得期待的莫过于注解和JIT了,对JIT感兴趣的可以看鸟哥的博客《PHP 8新特性之JIT简介》

PHP8引入了两个JIT编译引擎。跟踪JIT是这两种方法中最有前途的一种,它在综合基准测试上的性能提高了大约3倍,在某些特定的长时间运行的应用程序上性能提高了1.5到2倍。典型的应用程序性能与PHP7.4不相上下。

JIT对PHP8性能的影响:

php8-scheme.svg

除此之外,还有一些类型系统和错误处理的改进、其他语法调整和改进以及新的类,接口和功能,详细的可以去 PHP官网 查看

这里值得一提的是Opaque objects,用来代替Curl、Gd、Sockets、OpenSSL、XMLWriter和XML扩展的资源类型

//PHP7

var_dump(is_resource(curl_init())); // true

//PHP8

var_dump(is_resource(curl_init())); // false

var_dump(is_object(curl_init())); // true

任何个人或团体,未经允许禁止转载本文:《PHP8正式版发布,带来了注解和JIT》,谢谢合作!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值