日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/4/16 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 8 jit,PHP8正式版发布,带来了注解和JIT 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PHP8 正式版已經發布,它引入了一些重大變更,以及許多新特性和性能優化,包括命名參數、聯合類型、注解、Constructor Property Promotion、match 表達式、nullsafe 運算符、JIT,以及對類型系統、錯誤處理和一致性的改進。

之前的PHPCon上聽過Nikic的一些分享,感興趣的小伙伴可以查看Nikic的PPT

在 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性能的影響:

除此之外,還有一些類型系統和錯誤處理的改進、其他語法調整和改進以及新的類,接口和功能,詳細的可以去 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》,謝謝合作!

總結

以上是生活随笔為你收集整理的php 8 jit,PHP8正式版发布,带来了注解和JIT的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。