色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

PHP 8新特性簡介

瀏覽:118日期:2022-09-09 11:49:47

PHP 8新特性

新的主要PHP版本PHP 8預計將于2020年底發布。它現在處于非常活躍的開發階段,所以在接下來的幾個月里,事情可能會發生很大的變化。

在這篇文章中,我將持續更新預期的內容列表:新特性、性能改進和重大變化。因為PHP 8是一個新的主版本,所以您的代碼被破壞的幾率更高。如果你一直在更新最新的版本,升級應該不會太困難,因為大多數有破壞性的更改在7之前就已經廢棄了。*版本。

除了中斷更改之外,PHP 8還帶來了一些不錯的新特性,比如JIT編譯器和union類型;還有更多!

Union types:聯合類型

考慮到PHP的動態類型化特性,在很多情況下聯合類型是有用的。聯合類型是兩個或多個類型的集合,這些類型表示其中一個可以使用。

public function foo(Foo|Bar $input): int|float;

注意,void永遠不能是union類型的一部分,因為它表示“根本沒有返回值”。此外,可以使用|null來編寫可為空的聯合,也可以使用現有的?符號:

public function foo(Foo|null $foo): void;public function bar(?Bar $bar): void;

JIT

即時編譯器承諾顯著的性能改進,盡管并不總是在web請求的上下文中。目前還沒有任何準確的基準,但它們肯定會到來。

Static return type:靜態的返回類型

雖然已經可以返回self,但靜態類型直到PHP 8才成為有效的返回類型。考慮到PHP的動態類型特性,這一特性對許多開發人員都很有用。

class Foo{ public function test(): static { return new static(); }}

Weak maps

在PHP 7.4中添加的weakrefs RFC的基礎上,在PHP 8中添加了WeakMap實現。弱映射包含對對象的引用,這并不會阻止那些對象被垃圾收集。

以orm為例,它們通常實現保存對實體類的引用的緩存,以改進實體之間關系的性能。這些實體對象不能被垃圾回收,只要這個緩存有一個對它們的引用,即使緩存是唯一引用它們的東西。

如果這個緩存層使用弱引用和映射,那么PHP將在沒有其他對象引用它們時對這些對象進行垃圾收集。尤其是orm,它可以在一個請求中管理數百個(如果不是數千個)實體;弱映射為處理這些對象提供了一種更好的、對資源更友好的方法。

下面是弱映射的樣子,一個來自RFC的例子:

class Foo { private WeakMap $cache; public function getSomethingWithCaching(object $obj): object { return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj); }}

::class on objects

一個小而有用的新特性:現在可以在對象上使用::class,而不必在對象上使用get_class()。它的工作方式與get_class()相同。

$foo = new Foo();var_dump($foo::class);

Stringable interface

Stringable接口可用于鍵入提示任何字符串或實現了 tostring()的內容。而且,無論何時類實現了 tostring(),它都會在后臺自動實現接口,不需要手動實現。

class Foo{ public function __toString(): string { return ’foo’; }}function bar(Stringable $stringable) { /* … */ }bar(new Foo());bar(’abc’);

從接口創建DateTime對象

您已經可以使用DateTime:: createfromimmutabledatetime ($immutableDateTime)從一個datetime對象創建一個DateTime對象,但是另一種方法比較麻煩。通過添加DateTime::createFromInterface()和datetime::createFromInterface(),現在就有了一種將DateTime和datetime對象相互轉換的通用方法。

DateTime::createFromInterface(DateTimeInterface $other);DateTimeImmutable::createFromInterface(DateTimeInterface $other);

重新定義引擎的警告

許多以前只觸發警告或通知的錯誤現在已經轉換為正確的錯誤。以下警告已更改。

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新特性簡介的詳細內容,更多關于php 8新特性的資料請關注好吧啦網其它相關文章!

標簽: PHP
相關文章:
主站蜘蛛池模板: 成年人一级片 | 国产成人综合网亚洲欧美在线 | 国产成人午夜极速观看 | 亚洲精品影院久久久久久 | 欧美一级成人 | 精品午夜国产在线观看不卡 | 欧美白人最猛性xxxxx | 国产欧美日韩在线一区二区不卡 | 国产欧美在线观看 | 精品视频一区在线观看 | 免费一级a毛片在线 | 国产欧美日韩在线人成aaaa | 黄色亚洲网站 | 国内精品久久久久影院网站 | 在线高清免费爱做网 | 性感毛片 | 免费久久精品视频 | 69国产成人综合久久精品91 | 九九热播视频 | 国产大片线上免费看 | 久久久99精品免费观看精品 | 日韩亚洲欧美一区 | 在线看国产 | 欧美午夜精品久久久久久黑人 | 中国一级毛片aaa片 中国一级毛片录像 | 5388国产亚洲欧美在线观看 | 日本一级爽毛片在线看 | 特级毛片aaa免费版 特级毛片a级毛免费播放 | 一级成人a毛片免费播放 | 欧美大尺度aaa级毛片 | 中文字幕一二区 | 国产欧美日韩在线观看精品 | 国产精品三级 | 91国内视频 | 亚洲精品高清国产一久久 | 精品国产一区二区三区不卡在线 | 欧美日韩永久久一区二区三区 | 一级一黄在线观看视频免费 | 日本aaaa片毛片免费 | 美女午夜影院 | 亚洲精品aaa |