<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之旅 廣告
                ## Coroutine::defer `defer`用于資源的釋放, 會在**協程關閉之前**(即協程函數執行完畢時)進行調用, 就算拋出了異常, 已注冊的`defer`也會被執行. 需要注意的是, 它的調用順序和`go`語言中的`defer`一樣是逆序的(先進后出), 也就是先注冊`defer`的后執行, 在底層`defer`列表是一個`stack`, 先進后出. 逆序符合資源釋放的正確邏輯, 后申請的資源可能是基于先申請的資源的, 如先釋放先申請的資源, 后申請的資源可能就難以釋放。 與`Go`語言不同的是`Swoole4`的`defer`是協程退出前執行。這是因為`Go`沒有提供析構方法,而`PHP`對象有析構函數,使用`__destruct`就可以實現`Go`的`defer`特性,參見[如何實現 Go defer](https://wiki.swoole.com/wiki/page/p-go_defer.html)。 ## 示例 > Context為用戶自己實現的協程上下文管理器 ~~~ go(function () { $info = Context::get('info', Co::getuid()); // get context of this coroutine defer(function () { Context::delete('info', Co::getuid()); // delete }); throw new Exception('something wrong'); echo "never here\n"; }); ~~~ ## Context ~~~ use Swoole\Coroutine; class Context { protected static $pool = []; public static function cid():int { return Coroutine::getuid(); } public static function get($key, int $cid = null) { $cid = $cid ?? Coroutine::getuid(); if ($cid < 0) { return null; } if (isset(self::$pool[$cid][$key])) { return self::$pool[$cid][$key]; } return null; } public static function put($key, $item, int $cid = null) { $cid = $cid ?? Coroutine::getuid(); if ($cid > 0) { self::$pool[$cid][$key] = $item; } return $item; } public static function delete($key, int $cid = null) { $cid = $cid ?? Coroutine::getuid(); if ($cid > 0) { unset(self::$pool[$cid][$key]); } } public static function destruct(int $cid = null) { $cid = $cid ?? Coroutine::getuid(); if ($cid > 0) { unset(self::$pool[$cid]); } } } ~~~
                  <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>

                              哎呀哎呀视频在线观看