<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          PHP8 正式版發(fā)布了

          共 3374字,需瀏覽 7分鐘

           ·

          2020-12-07 23:20

          PHP8.0 是 PHP 的一個大版本更新。它包含許多新功能和優(yōu)化,包括命名參數(shù),聯(lián)合類型,屬性,構(gòu)造函數(shù)屬性提升,匹配表達(dá)式,nullsafe 運算符,JIT,以及類型系統(tǒng)的改進,錯誤處理和一致性。


          01

          命名參數(shù)


          PHP7

          htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);


          PHP8

          htmlspecialchars($string, double_encode: false);


          • 僅指定必需的參數(shù),跳過可選的參數(shù)。

          • 參數(shù)是 order-independent 和 self-documented。


          02

          屬性


          PHP7

          class PostsController{    /**     * @Route("/api/posts/{id}", methods={"GET"})     */    public function get($id) { /* ... */ }}


          PHP8

          class PostsController{    #[Route("/api/posts/{id}", methods: ["GET"])]    public function get($id) { /* ... */ }}


          現(xiàn)在,您可以使用具有PHP native 語法的結(jié)構(gòu)化元數(shù)據(jù)來代替 PHPDoc 注釋。


          03

          構(gòu)造函數(shù)屬性提升


          PHP7

          class Point {  public float $x;  public float $y;  public float $z;
          public function __construct( float $x = 0.0, float $y = 0.0, float $z = 0.0, ) { $this->x = $x; $this->y = $y; $this->z = $z; }}


          PHP8

          class Point {  public function __construct(    public float $x = 0.0,    public float $y = 0.0,    public float $z = 0.0,  ) {}}


          更少的?boilerplate?代碼來定義和初始化屬性。


          04

          聯(lián)合類型


          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


          可以使用在運行時驗證的?native?聯(lián)合類型聲明來代替類型組合的 PHPDoc 注釋。


          05

          匹配表達(dá)式


          PHP7

          switch (8.0) {  case '8.0':    $result = "Oh no!";    break;  case 8.0:    $result = "This is what I expected";    break;}echo $result;//> Oh no!


          PHP8

          echo match (8.0) {  '8.0' => "Oh no!",  8.0 => "This is what I expected",};//> This is what I expected


          新的匹配表達(dá)式類似于switch,并具有以下功能:

          • Match 是一個表達(dá)式,表示其結(jié)果可以存儲在變量中或返回。

          • Match 分支僅支持單行表達(dá)式,不需要break,statement。

          • Match 做嚴(yán)格的比較。


          06

          nullsafe 運算符


          PHP7

          $country =  null;
          if ($session !== null) { $user = $session->user;
          if ($user !== null) { $address = $user->getAddress(); if ($address !== null) { $country = $address->country; } }}


          PHP8

          $country = $session?->user?->getAddress()?->country;


          現(xiàn)在,您可以使用帶有新的 nullsafe 運算符的調(diào)用鏈來代替空檢查條件。當(dāng)對鏈中一個元素的求值失敗時,整個鏈的執(zhí)行將中止,并且整個鏈的求值為空。


          07

          Saner 字符串與數(shù)字的比較


          PHP7

          0 == 'foobar' // true


          PHP8

          0 == 'foobar' // false


          與數(shù)字字符串進行比較時,PHP8 使用數(shù)字比較。否則,它將數(shù)字轉(zhuǎn)換為字符串并使用字符串比較。


          08

          內(nèi)部函數(shù)的一致類型錯誤


          PHP7

          strlen([]); // Warning: strlen() expects parameter 1 to be string, array given
          array_chunk([], -1); // Warning: array_chunk(): Size parameter expected to be greater than 0


          PHP8

          strlen([]); // TypeError: strlen(): Argument #1 ($str) must be of type string, array given
          array_chunk([], -1); // ValueError: array_chunk(): Argument #2 ($length) must be greater than 0


          現(xiàn)在,如果參數(shù)驗證失敗,大多數(shù)內(nèi)部函數(shù)將引發(fā) Error 異常。


          09

          Just-In-Time 編譯


          PHP8 引入了兩個 JIT 編譯引擎。Tracing JIT 是兩者中最有希望的,它在綜合基準(zhǔn)測試中的性能提高了大約 3 倍,在某些特定的長期運行的應(yīng)用程序中提高了 1.5–2 倍。典型的應(yīng)用程序性能與 PHP7.4 相當(dāng)。


          JIT 對PHP8 性能的貢獻

          d1599545fbc3a72c55feab3c6b1b3219.webp


          10

          類型系統(tǒng)和錯誤處理方面的改進


          • 對?arithmetic/bitwise operators?進行更嚴(yán)格的類型檢查

          • 抽象特征方法驗證

          • 正確的魔術(shù)方法簽名

          • Reclassified engine warnings

          • 不兼容的方法簽名的致命錯誤

          • @ 運算符不再使致命錯誤失效

          • 用私有方法繼承

          • Mixed type

          • 靜態(tài)返回類型

          • 內(nèi)部函數(shù)的類型

          • Opaque objects instead of resources for?Curl,?Gd,?Sockets,?OpenSSL,?XMLWriter, and?XMLextensions


          11

          其他語法調(diào)整和改進


          • 在參數(shù)列表中允許逗號結(jié)尾和閉包使用列表

          • Non-capturing catches

          • 可變語法調(diào)整

          • 將命名空間名稱視為單個令牌

          • Throw is now an expression

          • Allow ::class on objects


          12

          新的類,接口和函數(shù)


          • Weak Map?class

          • Stringable?interface

          • str_contains(),?str_starts_with(),?str_ends_with()

          • fdiv()

          • get_debug_type()

          • get_resource_id()

          • token_get_all()?object implementation





          參考資料:

          https://www.php.net/releases/8.0/en.php

          https://www.php.net/ChangeLog-8.php


          瀏覽 43
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产麻豆成人免费视频 | 天天干夜夜骑 | 韩国一级视频 | 久久午夜无码人妻精品蜜桃冫 | 免费精品99 |