命名參數屬性構造函數屬性提升聯合類型Match 表達式Nullsafe 操作符更合理的字符串與數值的比較內置函數的一致錯誤類型JIT 編譯類型系統和錯誤處理的改進其他的語法微調和改進新類、接口、和函數
PHP 8.0.0 Released!
26 Nov 2020 PHP 開發組宣布 PHP 8.0.0 可用。
PHP 8.0 是 PHP 語言的主要更新。它包含了許多新特性和優化,包括命名參數、聯合類型、屬性、構造函數屬性提升、匹配表達式、nullsafe 操作符、JIT,以及對類型系統、錯誤處理和一致性的改進。
命名參數Named arguments
PHP 7
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
PHP 8
htmlspecialchars($string, double_encode: false);
只指定必需參數,跳過可選參數。
參數是獨立於順序並且是自描述的。
屬性Attributes
屬性,在很多語言中亦稱作註解。
PHP 7
class PostsController
{
/**
* @Route("/api/posts/{id}", methods={"GET"})
*/
public function get($id) { /* ... */ }
}
PHP 8
class PostsController
{
#[Route("/api/posts/{id}", methods: ["GET"])]
public function get($id) { /* ... */ }
}
不再使用 PHPDoc 注釋,現在可以使用 PHP 原生語法的結構化元數據。
構造函數屬性提升Constructor property promotion
PHP 7
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;
}
}
PHP 8
class Point {
public function __construct(
public float $x = 0.0,
public float $y = 0.0,
public float $z = 0.0,
) {}
}
定義和初始化屬性可使用更少的樣板代碼。
聯合類型Union types
PHP 7
class Number {
/** @var int|float */
private $number;
/**
* @param float|int $number
*/
public function __construct($number) {
$this->number = $number;
}
}
new Number('NaN'); // Ok
PHP 8
class Number {
public function __construct(
private int|float $number
) {}
}
new Number('NaN'); // TypeError
可使用聯合類型聲明在運行時驗證類型來代替使用 PHPDoc 注釋說明。
Match 表達式Match expression
PHP 7
switch (8.0) {
case '8.0':
$result = "Oh no!";
break;
case 8.0:
$result = "This is what I expected";
break;
}
echo $result;
//> Oh no!
PHP 8
echo match (8.0) {
'8.0' => "Oh no!",
8.0 => "This is what I expected",
};
//> This is what I expected
match 類似於 switch,具有如下特性:
Nullsafe 操作符Nullsafe operator
PHP 7
// PHP 7
$country = null;
if ($session !== null) {
$user = $session->user;
if ($user !== null) {
$address = $user->getAddress();
if ($address !== null) {
$country = $address->country;
}
}
}
PHP 8
// PHP 8
$country = $session?->user?->getAddress()?->country;
可使用 nullsafe 操作符完成鏈式調用來代替 null 的驗證。當對鏈中的一個元素求值失敗時,整個鏈的執行將中止,整個鏈的求值為 null。
更合理的字符串與數值的比較Saner string to number comparisons
PHP 7
// PHP 7
0 == 'foobar' // true
PHP 8
// PHP 8
0 == 'foobar' // false
當數值與數值字符串比較時,PHP 8 使用數值比較。否則將數值轉換為字符串並採用字符串比較。
內置函數的一致錯誤類型Consistent type errors for internal functions
PHP 7
// 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
// 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
大部分內置函數在參數驗證失敗後會拋出 Error 異常。
JIT 編譯Just-In-Time compilation
PHP 8 引入了兩個 JIT 編譯引擎。Tracing JIT 是其中最有前景的,它在合成基準測試上的性能提高了3倍,在一些特定的長時間運行的應用程式上的性能提高了1.5-2倍。典型的應用程式性能與 PHP 7.4 相當。
(圖像來自,https://www.php.net/images/php8/scheme.svg)
類型系統和錯誤處理的改進Type system and error handling improvements
算術/位操作中更嚴格的類型檢查 RFC
抽象 trait 方法驗證 RFC
魔術方法的正確籤名 RFC
重分類的引擎警告 RFC
不兼容方法籤名的致命錯誤 RFC
@ 操作符不在靜默致命錯誤。
私有方法的繼承 RFC
混合類型 RFC
靜態返回類型 RFC
內部函數 email_thread 的類型
用於代替 Curl, Gd, Sockets, OpenSSL, XMLWriter 和 XML 擴展資源的封裝對象
其他的語法微調和改進Other syntax tweaks and improvements
新類、接口、和函數New Classes, Interfaces, and Functions·
Weak Map 類
Stringable 接口
str_contains(), str_starts_with(), str_ends_with()
fdiv()
get_debug_type()
get_resource_id()
token_get_all() 對象實現
New DOM Traversal and Manipulation APIs