PHP 8.0 是 PHP 語(yǔ)言的一個(gè)主版本更新。
它包含了很多新功能與優(yōu)化項(xiàng), 包括命名參數(shù)、聯(lián)合類型、注解、構(gòu)造器屬性提升、match 表達(dá)式、nullsafe 運(yùn)算符、JIT,并改進(jìn)了類型系統(tǒng)、錯(cuò)誤處理、語(yǔ)法一致性。
1. 命名參數(shù)?RFC
在php7 中:
htmlspecialchars($string,ENT_COMPAT|ENT_HTML401,'UTF-8',false);
在php8 中:
htmlspecialchars($string, double_encode: false);
僅僅指定必填參數(shù),跳過(guò)可選參數(shù)。
參數(shù)的順序無(wú)關(guān)、自己就是文檔(self-documented)
2. 注解?RFC
在php7 中:
classPostsController{/**? ? * @Route("/api/posts/{id}", methods={"GET"})? ? */publicfunctionget($id){/* ... */}}
在php8 中:
classPostsController{#[Route("/api/posts/{id}", methods: ["GET"])]publicfunctionget($id){/* ... */}}
現(xiàn)在可以用 PHP 原生語(yǔ)法來(lái)使用結(jié)構(gòu)化的元數(shù)據(jù),而非 PHPDoc 聲明。
3. 構(gòu)造器屬性提升?RFC
在php7 中:
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;}}
在php8 中:
classPoint{publicfunction__construct(publicfloat$x=0.0,publicfloat$y=0.0,publicfloat$z=0.0,){}}
更少的樣板代碼來(lái)定義并初始化屬性。
4. 聯(lián)合類型?RFC
在php7 中:
classNumber{/** @var int|float */private$number;/**? * @param float|int $number? */publicfunction__construct($number){$this->number=$number;}}newNumber('NaN');// Ok
在php8 中:
classNumber{publicfunction__construct(privateint|float$number){}}newNumber('NaN');// TypeError
相對(duì)于以前的 PHPDoc 聲明類型的組合, 現(xiàn)在可以用原生支持的聯(lián)合類型聲明取而代之,可在實(shí)際運(yùn)行中驗(yàn)證。
5. Match 表達(dá)式?RFC 文檔
在php7 中:
switch(8.0){case'8.0':$result="Oh no!";break;case8.0:$result="This is what I expected";break;}echo$result;//> Oh no!
在php8 中:
echomatch(8.0){'8.0'=>"Oh no!",8.0=>"This is what I expected",};//> This is what I expected
新的 match 類似于 switch,并具有以下功能:
Match 是一個(gè)表達(dá)式,它可以儲(chǔ)存到變量中亦可以直接返回。
Match 分支僅支持單行,它不需要一個(gè) break; 語(yǔ)句。
Match 使用嚴(yán)格比較。
6. Nullsafe 運(yùn)算符?RFC
在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;
現(xiàn)在可以用新的 nullsafe 運(yùn)算符鏈?zhǔn)秸{(diào)用,而不需要條件檢查 null。 如果鏈條中的一個(gè)元素失敗了,整個(gè)鏈條會(huì)中止并認(rèn)定為 Null。
7. 字符串與數(shù)字的比較更符合邏輯?RFC
在php7 中:
0=='foobar'// true
在php8 中:
0=='foobar'// false
PHP 8 比較數(shù)字字符串(numeric string)時(shí),會(huì)按數(shù)字進(jìn)行比較。 不是數(shù)字字符串時(shí),將數(shù)字轉(zhuǎn)化為字符串,按字符串比較。
8. 內(nèi)部函數(shù)類型錯(cuò)誤的一致性。?RFC
在php7 中:
strlen([]);// Warning: strlen() expects parameter 1 to be string, array givenarray_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 givenarray_chunk([],-1);// ValueError: array_chunk(): Argument #2 ($length) must be greater than 0
現(xiàn)在大多數(shù)內(nèi)部函數(shù)在參數(shù)驗(yàn)證失敗時(shí)拋出 Error 級(jí)異常。
9. 即時(shí)編譯
PHP 8 引入了兩個(gè)即時(shí)編譯引擎。 Tracing JIT 在兩個(gè)中更有潛力,它在綜合基準(zhǔn)測(cè)試中顯示了三倍的性能, 并在某些長(zhǎng)時(shí)間運(yùn)行的程序中顯示了 1.5-2 倍的性能改進(jìn)。 典型的應(yīng)用性能則和 PHP 7.4 不相上下。
關(guān)于 JIT 對(duì) PHP 8 性能的貢獻(xiàn)

10. 類型系統(tǒng)與錯(cuò)誤處理的改進(jìn)
算術(shù)/位運(yùn)算符更嚴(yán)格的類型檢測(cè)?RFC
Abstract trait 方法的驗(yàn)證?RFC
確保魔術(shù)方法簽名正確?RFC
PHP 引擎 warning 警告的重新分類?RFC
不兼容的方法簽名導(dǎo)致 Fatal 錯(cuò)誤?RFC
操作符 @ 不再抑制 fatal 錯(cuò)誤。
私有方法繼承?RFC
Mixed 類型?RFC
Static 返回類型?RFC
內(nèi)部函數(shù)的類型?Email thread
擴(kuò)展?Curl、?Gd、?Sockets、?OpenSSL、?XMLWriter、?XML?以 Opaque 對(duì)象替換 resource。
11. 其他語(yǔ)法調(diào)整和改進(jìn)
允許參數(shù)列表中的末尾逗號(hào)?RFC、 閉包 use 列表中的末尾逗號(hào)?RFC
無(wú)捕獲的 catche?RFC
變量語(yǔ)法的調(diào)整?RFC
Namespace 名稱作為單個(gè) token?RFC
現(xiàn)在 throw 是一個(gè)表達(dá)式?RFC
允許對(duì)象的 ::class?RFC
12. 新的類、接口、函數(shù)
Weak Map?類
Stringable?接口
str_contains()、?str_starts_with()、str_ends_with()
token_get_all()?對(duì)象實(shí)現(xiàn)
13. 幾個(gè)棄用
在 PHP 7. * 的開(kāi)發(fā)期間,添加了幾個(gè)棄用版本,這些棄用已于 PHP 8 最終確定。
PHP 7.2?中的棄用
PHP 7.3?中的棄用
PHP 7.4?中的棄用
總結(jié):以上就是這篇文章的全部?jī)?nèi)容了。