PHP 7.4.0 發(fā)布了,此版本標志著 PHP 7 系列的第四次特性更新。
PHP 7.4.0 進行了許多改進,并帶來了一些新特性,包括:
1.Typed Properties 類型屬性
類屬性現(xiàn)在支持類型聲明,以下示例將強制 $User-> id 只能分配 int 值,而 $User-> name 只能分配 string 值。
<?php class User { public int $id; public string $name; } ?>
2.Arrow Functions 箭頭函數(shù)
箭頭函數(shù)提供了用于定義具有隱式按值作用域綁定的函數(shù)的簡寫語法。
<?php $factor = 10; $nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]); // $nums = array(10, 20, 30, 40); ?>
將閉包傳遞給 array_map 或 array_filter 等函數(shù)時,它可以發(fā)揮極大的作用。
// A collection of Post objects $posts = [/* … */]; $ids = array_map(fn($post) => $post->id, $posts);
3.Limited Return Type Covariance and Argument Type Contravariance 有限返回類型協(xié)變與參數(shù)類型逆變
僅當使用自動加載時,才提供完全協(xié)變/逆變支持。在單個文件中,只能使用非循環(huán)類型引用,因為所有類在被引用之前都必須可用。
<?php class A {} class B extends A {} class Producer { public function method(): A {} } class ChildProducer extends Producer { public function method(): B {} } ?>
4.Unpacking Inside Arrays 打包內(nèi)部數(shù)組
<?php $parts = ['apple', 'pear']; $fruits = ['banana', 'orange', ...$parts, 'watermelon']; // ['banana', 'orange', 'apple', 'pear', 'watermelon']; ?>
5.Numeric Literal Separator 數(shù)值文字分隔符
數(shù)字文字可以在數(shù)字之間包含下劃線。
<?php 6.674_083e-11; // float 299_792_458; // decimal 0xCAFE_F00D; // hexadecimal 0b0101_1111; // binary ?>
6.Weak References 弱引用
弱引用使程序員可以保留對對象的引用,不會阻止對象被銷毀。
7.Allow Exceptions from __toString() 允許從 __toString() 拋出異常
現(xiàn)在允許從 __toString() 引發(fā)異常,以往這會導致致命錯誤,字符串轉(zhuǎn)換中現(xiàn)有的可恢復致命錯誤已轉(zhuǎn)換為 Error 異常。
8.Opcache Preloading Opcache 預加載
新增 Opcache 預加載支持。
此外還有一些棄用,以及從核心中刪除一些擴展,詳情查看:
https://www.php.net
https://www.php.net/manual/zh/migration74.new-features.php
- 上一篇:5款強大的密碼顯示/隱藏效果推薦(附源碼下載)
- 下一篇:沒有了