PHP 8新特性

新的主要PHP版本PHP 8預計將於2020年末發佈。它如今處於很是活躍的開發階段,因此在接下來的幾個月裏,事情可能會發生很大的變化。php

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

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

Union types:聯合類型

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

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請求的上下文中。目前尚未任何準確的基準,但它們確定會到來。this

Static return type:靜態的返回類型

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

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

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

以orm爲例,它們一般實現保存對實體類的引用的緩存,以改進實體之間關係的性能。這些實體對象不能被垃圾回收,只要這個緩存有一個對它們的引用,即便緩存是惟一引用它們的東西。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
相關文章
相關標籤/搜索