PHP 7.4.0 發布了,此版本標誌著 PHP 7 系列的第四次特性更新。
PHP 7.4.0 進行了許多改進,並帶來了一些新特性,包括:
Typed Properties 類型屬性
類屬性現在支持類型聲明,以下示例將強制 $User-> id 只能分配 int 值,而 $User-> name 只能分配 string 值。
<?phpclass User { public int $id; public string $name;}?>
Arrow Functions 箭頭函數
箭頭函數提供了用於定義具有隱式按值作用域綁定的函數的簡寫語法。
<?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 有限返回類型協變與參數類型逆變
僅當使用自動加載時,才提供完全協變/逆變支持。在單個文件中,只能使用非循環類型引用,因為所有類在被引用之前都必須可用。
<?phpclass A {}class B extends A {}class Producer { public function method(): A {}}class ChildProducer extends Producer { public function method(): B {}}?>
Unpacking Inside Arrays 打包內部數組
<?php$parts = ['apple', 'pear'];$fruits = ['banana', 'orange', ...$parts, 'watermelon'];// ['banana', 'orange', 'apple', 'pear', 'watermelon'];?>
Numeric Literal Separator 數值文字分隔符
數字文字可以在數字之間包含下劃線。
<?php6.674_083e-11; // float299_792_458; // decimal0xCAFE_F00D; // hexadecimal0b0101_1111; // binary?>
Weak References 弱引用
弱引用使程式設計師可以保留對對象的引用,不會阻止對象被銷毀。
Allow Exceptions from __toString() 允許從 __toString() 拋出異常
現在允許從 __toString() 引發異常,以往這會導致致命錯誤,字符串轉換中現有的可恢復致命錯誤已轉換為 Error 異常。
Opcache Preloading Opcache 預加載
新增 Opcache 預加載支持。
此外還有一些棄用,以及從核心中刪除一些擴展,詳情查看:https://www.php.net/manual/zh/migration74.new-features.php#migration74.new-features.core.unpack-inside-array
本文原標題:PHP 7.4.0 發布
本文原地址:https://www.oschina.net/news/111724/php-7-4-0-released
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請儘快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯繫客服。電話:028-62778877-8261;郵箱:jenny@west.cn。本站原創內容未經允許不得轉載,或轉載時需註明出處::西部數碼資訊門戶 » PHP 7.4.0 發布 支持類型聲明