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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/1931.html
標籤:PHP
上一篇:php亂數生成
下一篇:寶塔面板搭建個人網站
