<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                https://www.php.net/manual/zh/functions.anonymous.php Laravel中的IoC服務容器中,大量使用了閉包能力,我們模擬一個便于大家理解。當然,更好的方案是自己去翻翻Laravel的源碼。 ~~~html class B {} class C {} class D {} class Ioc { public $objs = []; public $containers = []; public function __construct() { $this->objs['b'] = function () { return new B(); }; $this->objs['c'] = function () { return new C(); }; $this->objs['d'] = function () { return new D(); }; } public function bind($name) { if (!isset($this->containers[$name])) { if (isset($this->objs[$name])) { $this->containers[$name] = $this->objs[$name](); } else { return null; } } return $this->containers[$name]; } } $ioc = new Ioc(); $bClass = $ioc->bind('b'); $cClass = $ioc->bind('c'); $dClass = $ioc->bind('d'); $eClass = $ioc->bind('e'); var_dump($bClass); // B var_dump($cClass); // C var_dump($dClass); // D var_dump($eClass); // NULL ~~~ > 總結 閉包特性經常出現的地方是事件回調類的功能中,另外就是像彩蛋中的IoC的實現。因為閉包有一個很強大的能力就是可以延遲加載。IoC的例子我們的閉包中返回的是新new出來的對象。當我們的程序運行的時候,如果沒有調用$ioc->bind('b'),那么這個B對象是不會創建的,也就是說這時它還不會占用資源占用內存。而當我們需要的時候,從服務容器中拿出來的時候才利用閉包真正的去創建對象。同理,事件的回調也是一樣的概念。事件發生時在我們需要處理的時候才去執行回調里面的代碼。如果沒有閉包的概念,那么$objs容器就這么寫了: ~~~html $this->objs['b'] = new B(); $this->objs['c'] = new C(); $this->objs['d'] = new D(); ~~~ 容器在實例化的時候就把所有的類都必須實例化了。這樣對于程序來說很多用不上的對象就都被創建了,帶來非常大的資源浪費。 基于閉包的這種強大能力,現在閉包函數已經在Laravel、TP6等框架中無處不在了。學習無止盡,掌握原理再去學習框架往往更能事半功倍。 測試代碼:[https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/%E8%BF%98%E4%B8%8D%E7%9F%A5%E9%81%93PHP%E6%9C%89%E9%97%AD%E5%8C%85%EF%BC%9F%E9%82%A3%E4%BD%A0%E7%9C%9FOUT%E4%BA%86.php](https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/%E8%BF%98%E4%B8%8D%E7%9F%A5%E9%81%93PHP%E6%9C%89%E9%97%AD%E5%8C%85%EF%BC%9F%E9%82%A3%E4%BD%A0%E7%9C%9FOUT%E4%BA%86.php) 參考文檔:[https://www.php.net/manual/zh/functions.anonymous.php] (https://www.php.net/manual/zh/functions.anonymous.php) [https://www.php.net/manual/zh/functions.anonymous.php#100545] (https://www.php.net/manual/zh/functions.anonymous.php#100545) [https://www.php.net/manual/zh/functions.anonymous.php#119388] (https://www.php.net/manual/zh/functions.anonymous.php#119388)
                  <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>

                              哎呀哎呀视频在线观看