<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                > PHP 8.0.0 已經正式發布了,這個對于PHPer無疑是一個令人振奮的消息。它包含了很多新功能與優化項, 包括命名參數、聯合類型、注解、構造器屬性提升、`match `表達式、`nullsafe `運算符、`JIT`,并改進了類型系統、錯誤處理、語法一致性。 ## 最人性化的特性:命名參數、聯合類型、`mixed`類型 這幾個新特性讓PHP在強類型方面進一步完善,而且對PHPDoc的注釋依賴越來越弱,代碼即文檔的好處是開發者最頭疼的事情終于有辦法可以偷懶了。 ### 命名參數 命名參數可以讓函數或者方法的調用更加清晰直觀,對于如下的函數定義 ``` function foo(string $a, string $b, ?string $c = null, ?string $d = null) { /* … */ } ``` 你可以通過下面的方式傳入參數進行調用 ``` foo( b: 'value b', a: 'value a', d: 'value d', ); ``` 最大的好處是傳入參數的順序是和定義無關的,而且還可以混合傳參(但不建議)。 ### 聯合類型 相對于以前的 PHPDoc 聲明類型的組合, 現在可以用原生支持的聯合類型聲明取而代之,可在實際運行中驗證。 PHP7 ``` class Number { /** @var int|float */ private $number; /** * @param float|int $number */ public function __construct($number) { $this->number = $number; } } new Number('NaN'); // Ok ``` PHP8 ``` class Number { public function __construct( private int|float $number ) {} } new Number('NaN'); // TypeError ``` ### 新的 `mixed`類型 `mixed`本身是以下類型之一: * `array` * `bool` * `callable` * `int` * `float` * `null` * `object` * `resource` * `string` 注意,`mixed`也可以用作參數或屬性類型,而不僅僅是返回類型。 另外由于`mixed`已經包含`null`,因此不允許將其設置為`nullable`。以下內容將觸發錯誤: ~~~ // Fatal error: Mixed types cannot be nullable, null is already part of the mixed type. function bar(): ?mixed {} ~~~ ## 最具貢獻的特性:`JIT` JIT作為PHP底層編譯引擎,對于PHP8的性能貢獻是非常之大,不過對于常規WEB應用來說,優勢不明顯,但仍然是非常的高大上特性,是PHP8的扛鼎之作。 PHP 8 引入了兩個即時編譯引擎。 Tracing JIT 在兩個中更有潛力,它在綜合基準測試中顯示了三倍的性能, 并在某些長時間運行的程序中顯示了 1.5-2 倍的性能改進。 典型的應用性能則和 PHP 7.4 不相上下。 ### 關于 JIT 對 PHP 8 性能的貢獻 ![Just-In-Time compilation](https://www.php.net/images/php8/scheme.svg) ## 最實用的特性:構造器屬性提升、`Nullsafe`運算符、`str_contains()`、 `str_starts_with()`、 `str_ends_with()` ### 構造器屬性提升 這個新的語法糖來用來創建值對象或數據傳輸對象。不用為類屬性和構造函數指定它們,PHP 現在可以將它們合并為一個。 代替如下代碼: ``` class Money { public Currency $currency; public int $amount; public function __construct( Currency $currency, int $amount, ) { $this->currency = $currency; $this->amount = $amount; } } ``` 你可以這樣做: ``` class Money { public function __construct( public Currency $currency, public int $amount, ) {} } ``` ### `nullsafe`運算符 現在可以用新的 nullsafe 運算符鏈式調用,而不需要條件檢查 null。 如果鏈條中的一個元素失敗了,整個鏈條會中止并認定為 Null。 ``` $country = null; if ($session !== null) { $user = $session->user; if ($user !== null) { $address = $user->getAddress(); if ($address !== null) { $country = $address->country; } } } ``` 簡化為一行代碼 ``` $country = $session?->user?->getAddress()?->country; ``` 確實是有點酷 ### `str_contains()`、`str_starts_with()`和`str_ends_with()`函數 有些人可能會說它早就該有了,但我們終于不必再依賴`strpos()` 來知道字符串是否包含另一個字符串了。 代替如下: ``` if (strpos('string with lots of words', 'words') !== false) { /* … */ } ``` 你可以這樣做 ``` if (str_contains('string with lots of words', 'words')) { /* … */ } ``` 感覺大多數場景應該是不需要使用`strpos`了吧,外兩個早就應該有了,`str_starts_with()`和`str_ends_with()`這兩個函數現在能省事不少。 ~~~ str_starts_with('haystack', 'hay'); // true str_ends_with('haystack', 'stack'); // true ~~~ ## 最具潛力的特性:注解、`Match`表達式、`WeakMap` ### 注解 現在可以用原生的PHP語法來使用結構化的元數據,而不需要再依賴PHPDoc解析,性能也隨之提升。之前定義注解路由可能需要使用: ``` class PostsController { /** * @Route("/api/posts/{id}", methods={"GET"}) */ public function get($id) { /* ... */ } } ``` 現在你可以直接用PHP的注解語法來定義,并通過反射直接獲取 ``` class PostsController { #[Route("/api/posts/{id}", methods: ["GET"])] public function get($id) { /* ... */ } } ``` ### `Match`表達式 你可以稱它為switch表達式的大哥:match可以返回值,不需要break語句,可以組合條件,使用嚴格的類型比較,并且不執行任何類型的強制。 如下所示: ``` $result = match($input) { 0 => "hello", '1', '2', '3' => "world", }; ``` ### WeakMap `WeakMap`保留對對象的引用,這些引用不會阻止這些對象被垃圾回收。 以 ORM 為例,它們通常實現緩存,這些緩存保存對實體類的引用,以提高實體之間的關系性能。這些實體對象不能被垃圾回收,只要此緩存具有對它們的引用,即使緩存是唯一引用它們的對象。 如果此緩存層使用弱引用和映射代替,PHP 將垃圾收集這些對象當再沒有別的引用他們了。特別是在 ORM 的情況下,它可以管理請求中的數百個,如果不是數千個實體;weak maps可以提供更好、更資源友好的處理這些對象的方法。 下面是weak maps的示例: ``` class Foo { private WeakMap $cache; public function getSomethingWithCaching(object $obj): object { return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj); } } ``` ## 其它特性 ### 0 == 'foobar' 終于返回了`false` 我們知道在PHP7里面 ``` 0 == 'foobar' // 返回true ``` 現在終于看起來更比較符合邏輯了 ``` 0 == 'foobar' // 返回false ``` ### 可以在對象上使用`::class` 一個小而有用的新特性:現在可以對對象使用`::class`,它的工作方式與 `get_class()` 相同。 ``` $foo = new Foo(); var_dump($foo::class); ``` ### traits 中的抽象方法改進 Traits 可以指定抽象方法,這些方法必須由使用它們的類實現。在PHP8,必須保持一致的方法定義,包括參數類型和返回類型。 ``` trait MyTrait { abstract private function neededByTheTrait(): string; public function doSomething() { return strlen($this->neededByTheTrait()); } } class TraitUser { use MyTrait; // This is allowed: private function neededByTheTrait(): string { } // This is forbidden (incorrect return type) private function neededByTheTrait(): stdClass { } // This is forbidden (non-static changed to static) private static function neededByTheTrait(): string { } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看