PHP 7.4.0 發佈了,此版本標誌着 PHP 7 系列的第四次特性更新。php
PHP 7.4.0 進行了許多改進,並帶來了一些新特性,包括:程序員
Typed Properties 類型屬性數組
類屬性如今支持類型聲明,如下示例將強制 $User-> id 只能分配 int 值,而 $User-> name 只能分配 string 值。閉包
<?php
class User {
public int $id;
public string $name;
}
?>
複製代碼
Arrow Functions 箭頭函數app
箭頭函數提供了用於定義具備隱式按值做用域綁定的函數的簡寫語法。ide
<?php
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);
?>
複製代碼
將閉包傳遞給 array_map 或 array_filter 等函數時,它能夠發揮極大的做用。函數
// A collection of Post objects $posts = [/* … */];
$ids = array_map(fn($post) => $post->id, $posts);
複製代碼
Limited Return Type Covariance and Argument Type Contravariance 有限返回類型協變與參數類型逆變post
僅當使用自動加載時,才提供徹底協變/逆變支持。在單個文件中,只能使用非循環類型引用,由於全部類在被引用以前都必須可用。ui
<?php
class A {}
class B extends A {}
class Producer {
public function method(): A {}
}
class ChildProducer extends Producer {
public function method(): B {}
}
?>
複製代碼
Unpacking Inside Arrays 打包內部數組spa
<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
?>
複製代碼
Numeric Literal Separator 數值文字分隔符
數字文字能夠在數字之間包含下劃線。
<?php
6.674_083e-11; // float
299_792_458; // decimal
0xCAFE_F00D; // hexadecimal
0b0101_1111; // binary
?>
複製代碼
Weak References 弱引用
弱引用使程序員能夠保留對對象的引用,不會阻止對象被銷燬。
Allow Exceptions from __toString() 容許從 __toString() 拋出異常
如今容許從 __toString() 引起異常,以往這會致使致命錯誤,字符串轉換中現有的可恢復致命錯誤已轉換爲 Error 異常。
Opcache Preloading Opcache 預加載
新增 Opcache 預加載支持。
此外還有一些棄用,以及從核心中刪除一些擴展,詳情查看: