PHP 8新特性簡(jiǎn)介
PHP 8新特性
新的主要PHP版本PHP 8預(yù)計(jì)將于2020年底發(fā)布。它現(xiàn)在處于非?;钴S的開發(fā)階段,所以在接下來的幾個(gè)月里,事情可能會(huì)發(fā)生很大的變化。
在這篇文章中,我將持續(xù)更新預(yù)期的內(nèi)容列表:新特性、性能改進(jìn)和重大變化。因?yàn)镻HP 8是一個(gè)新的主版本,所以您的代碼被破壞的幾率更高。如果你一直在更新最新的版本,升級(jí)應(yīng)該不會(huì)太困難,因?yàn)榇蠖鄶?shù)有破壞性的更改在7之前就已經(jīng)廢棄了。*版本。
除了中斷更改之外,PHP 8還帶來了一些不錯(cuò)的新特性,比如JIT編譯器和union類型;還有更多!
Union types:聯(lián)合類型
考慮到PHP的動(dòng)態(tài)類型化特性,在很多情況下聯(lián)合類型是有用的。聯(lián)合類型是兩個(gè)或多個(gè)類型的集合,這些類型表示其中一個(gè)可以使用。
public function foo(Foo|Bar $input): int|float;
注意,void永遠(yuǎn)不能是union類型的一部分,因?yàn)樗硎尽案緵]有返回值”。此外,可以使用|null來編寫可為空的聯(lián)合,也可以使用現(xiàn)有的?符號(hào):
public function foo(Foo|null $foo): void; public function bar(?Bar $bar): void;
JIT
即時(shí)編譯器承諾顯著的性能改進(jìn),盡管并不總是在web請(qǐng)求的上下文中。目前還沒有任何準(zhǔn)確的基準(zhǔn),但它們肯定會(huì)到來。
Static return type:靜態(tài)的返回類型
雖然已經(jīng)可以返回self,但靜態(tài)類型直到PHP 8才成為有效的返回類型??紤]到PHP的動(dòng)態(tài)類型特性,這一特性對(duì)許多開發(fā)人員都很有用。
class Foo { public function test(): static { return new static(); } }
Weak maps
在PHP 7.4中添加的weakrefs RFC的基礎(chǔ)上,在PHP 8中添加了WeakMap實(shí)現(xiàn)。弱映射包含對(duì)對(duì)象的引用,這并不會(huì)阻止那些對(duì)象被垃圾收集。
以orm為例,它們通常實(shí)現(xiàn)保存對(duì)實(shí)體類的引用的緩存,以改進(jìn)實(shí)體之間關(guān)系的性能。這些實(shí)體對(duì)象不能被垃圾回收,只要這個(gè)緩存有一個(gè)對(duì)它們的引用,即使緩存是唯一引用它們的東西。
如果這個(gè)緩存層使用弱引用和映射,那么PHP將在沒有其他對(duì)象引用它們時(shí)對(duì)這些對(duì)象進(jìn)行垃圾收集。尤其是orm,它可以在一個(gè)請(qǐng)求中管理數(shù)百個(gè)(如果不是數(shù)千個(gè))實(shí)體;弱映射為處理這些對(duì)象提供了一種更好的、對(duì)資源更友好的方法。
下面是弱映射的樣子,一個(gè)來自RFC的例子:
class Foo { private WeakMap $cache; public function getSomethingWithCaching(object $obj): object { return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj); } }
::class on objects
一個(gè)小而有用的新特性:現(xiàn)在可以在對(duì)象上使用::class,而不必在對(duì)象上使用get_class()。它的工作方式與get_class()相同。
$foo = new Foo(); var_dump($foo::class);
Stringable interface
Stringable接口可用于鍵入提示任何字符串或?qū)崿F(xiàn)了 tostring()的內(nèi)容。而且,無論何時(shí)類實(shí)現(xiàn)了 tostring(),它都會(huì)在后臺(tái)自動(dòng)實(shí)現(xiàn)接口,不需要手動(dòng)實(shí)現(xiàn)。
class Foo { public function __toString(): string { return 'foo'; } } function bar(Stringable $stringable) { /* … */ } bar(new Foo()); bar('abc');
從接口創(chuàng)建DateTime對(duì)象
您已經(jīng)可以使用DateTime:: createfromimmutabledatetime ($immutableDateTime)從一個(gè)datetime對(duì)象創(chuàng)建一個(gè)DateTime對(duì)象,但是另一種方法比較麻煩。通過添加DateTime::createFromInterface()和datetime::createFromInterface(),現(xiàn)在就有了一種將DateTime和datetime對(duì)象相互轉(zhuǎn)換的通用方法。
DateTime::createFromInterface(DateTimeInterface $other); DateTimeImmutable::createFromInterface(DateTimeInterface $other);
重新定義引擎的警告
許多以前只觸發(fā)警告或通知的錯(cuò)誤現(xiàn)在已經(jīng)轉(zhuǎn)換為正確的錯(cuò)誤。以下警告已更改。
- Undefined variable: Error exception instead of notice
- Undefined array index: warning instead of notice
- Division by zero: DivisionByZeroError exception instead of warning
- Attempt to increment/decrement property ‘%s' of non-object: Error exception instead of warning
- Attempt to modify property ‘%s' of non-object: Error exception instead of warning
- Attempt to assign property ‘%s' of non-object: Error exception instead of warning
- Creating default object from empty value: Error exception instead of warning
- Trying to get property ‘%s' of non-object: warning instead of notice
- Undefined property: %s::$%s: warning instead of notice
- Cannot add element to the array as the next element is already occupied: Error exception instead of warning
- Cannot unset offset in a non-array variable: Error exception instead of warning
- Cannot use a scalar value as an array: Error exception instead of warning
- Only arrays and Traversables can be unpacked: TypeError exception instead of warning
- Invalid argument supplied for foreach(): TypeError exception instead of warning
- Illegal offset type: TypeError exception instead of warning
- Illegal offset type in isset or empty: TypeError exception instead of warning
- Illegal offset type in unset: TypeError exception instead of warning
- Array to string conversion: warning instead of notice
- Resource ID#%d used as offset, casting to integer (%d): warning instead of notice
- String offset cast occurred: warning instead of notice
- Uninitialized string offset: %d: warning instead of notice
- Cannot assign an empty string to a string offset: Error exception instead of warning
以上就是PHP 8新特性簡(jiǎn)介的詳細(xì)內(nèi)容,更多關(guān)于php 8新特性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php實(shí)現(xiàn)常見圖片格式的水印和縮略圖制作(面向?qū)ο?
這篇文章主要介紹了php實(shí)現(xiàn)常見圖片格式j(luò)pg,png,gif的水印和縮略圖制作,使用面向?qū)ο蠓椒▽?shí)現(xiàn)PHP圖片水印和縮略圖功能,感興趣的小伙伴們可以參考一下2016-06-06PHP json格式和js json格式 js跨域調(diào)用實(shí)現(xiàn)代碼
整理一個(gè)json格式的例子,以及php json格式與js json之間的調(diào)用(傳說中的js跨域調(diào)用)2012-09-09PHP實(shí)現(xiàn)導(dǎo)出excel數(shù)據(jù)的類庫用法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)導(dǎo)出excel數(shù)據(jù)的類庫用法,結(jié)合實(shí)例形式分析了php操作Excel數(shù)據(jù)的讀取與導(dǎo)出操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10PHP和Selenium搭建高效網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)技術(shù)探索
隨著信息時(shí)代的到來,網(wǎng)站被認(rèn)為是獲取信息的主要途徑之一,但是,手動(dòng)獲取網(wǎng)站上的信息是非常繁瑣的,因此出現(xiàn)了自動(dòng)抓取網(wǎng)頁的方式——網(wǎng)絡(luò)爬蟲,這篇文章將介紹如何使用PHP和Selenium搭建一個(gè)高效的網(wǎng)絡(luò)爬蟲來自動(dòng)收集信息2024-01-01PHP微信企業(yè)號(hào)開發(fā)之回調(diào)模式開啟與用法示例
這篇文章主要介紹了PHP微信企業(yè)號(hào)開發(fā)之回調(diào)模式開啟與用法,結(jié)合具體實(shí)例形式分析了php微信企業(yè)號(hào)回調(diào)模式開啟與使用方法相關(guān)操作技巧,代碼中備有詳盡的注釋說明便于讀者理解,需要的朋友可以參考下2017-11-11PHP計(jì)劃任務(wù)、定時(shí)執(zhí)行任務(wù)的實(shí)現(xiàn)代碼
PHP計(jì)劃任務(wù)、定時(shí)執(zhí)行任務(wù)的實(shí)現(xiàn)用到的函數(shù) ignore_user_abort(),set_time_limit(0),sleep($interval) 此代碼只要運(yùn)行一次后關(guān)閉瀏覽器即可。2011-04-04php設(shè)計(jì)模式 Template (模板模式)
定義一個(gè)操作中的算法骨架,而將一些步驟延遲到子類中,使得子類可以不改變一個(gè)算法的結(jié)構(gòu)可以定義該算法的某些特定步驟2011-06-06