欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          PHP中箭頭函數(shù)的實(shí)例詳解

          PHP中箭頭函數(shù)的實(shí)例詳解

          稱為箭頭函數(shù)的短閉包是PHP7.4版本將帶來的期待已久的功能之一。它是由 Nikita Popov、Levi Morrison 和 Bob Weinand 提出的,你可以在此處閱讀原 RFC

          相關(guān)學(xué)習(xí)推薦:PHP編程從入門到精通

          摘自 Doctrine DBAL 的快速示例

          //老辦法 $this->existingSchemaPaths = array_filter($paths, function ($v) use ($names) {     return in_array($v, $names); });  // 使用箭頭函數(shù)的新方法 $this->existingSchemaPaths = array_filter($paths, fn($v) => in_array($v, $names));

          讓我們來看看規(guī)則吧

          1. fn 是關(guān)鍵字,而不是保留的函數(shù)名稱。
          2. 它只能有一個(gè)表達(dá)式,那就是 return 語句。
          3. 不需要使用rereturnuse關(guān)鍵字。
          4. $this 變量,作用域和 LSB 作用域自動(dòng)綁定。
          5. 你可以鍵入提示參數(shù)和返回類型。
          6. 你甚至可以使用引用&和 展開操作符 ...

          幾個(gè)例子

          //作用域示例 $discount = 5; $items = array_map(fn($item) => $item - $discount, $items);  //類型提示 $users = array_map(fn(User $user): int => $user->id, $users);  //展開操作符 function complement(callable $f) {     return fn(...$args) => !$f(...$args); }  //嵌套 $z = 1; $fn = fn($x) => fn($y) => $x * $y + $z;  //有效的函數(shù)簽名 fn(array $x) => $x; fn(): int => $x; fn($x = 42) => $x; fn(&$x) => $x; fn&($x) => $x; fn($x, ...$rest) => $rest;

          未來范圍

          1. 多行箭頭函數(shù)
          2. 允許對(duì)類內(nèi)的函數(shù)使用箭頭函數(shù)。
          //現(xiàn)今 class Test {     public function method() {         $fn = fn() => var_dump($this);         $fn(); // object(Test)#1 { ... }          $fn = static fn() => var_dump($this);         $fn(); // Error: Using $this when not in object context     } }  //也許在未來的某一天 class Test {     private $foo;     private $bar;      fn getFoo() => $this->foo;     fn getBar() => $this->bar; }

          我最喜歡的要點(diǎn)

          1. 回調(diào)可以更短
          2. 不需要use關(guān)鍵字便問變量。

          讓我知道你對(duì)這些更新有什么看法,你最喜歡的收獲是什么?

          感謝閱讀。

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)