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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ``` function gen(){   while(true){     yield "gen\n";   } } $gen = gen(); var_dump($gen instanceof Iterator); echo "hello, world!"; ``` 如果事先沒了解過yield,可能會覺得這段代碼一定會進入死循環。但是我們將這段代碼直接運行會發現,輸出hello, world!,預想的死循環沒出現。 究竟是什么樣的力量,征服了while(true)呢,接下來就帶大家一起來領略一下yield關鍵字的魅力。 首先要從foreach說起,我們都知道對象,數組和對象可以被foreach語法遍歷,數字和字符串缺不行。其實除了數組和對象之外PHP內部還提供了一個 Iterator 接口,實現了Iterator接口的對象,也是可以被foreach語句遍歷,當然跟普通對象的遍歷就很不一樣了。 以下面的代碼為例: ``` class Number implements Iterator{   protected $key;   protected $val;   protected $count;   public function __construct(int $count){     $this->count = $count;   }   public function rewind(){     $this->key = 0;     $this->val = 0;   }   public function next(){   $this->key += 1;   $this->val += 2;   }   public function current(){     return $this->val;   }   public function key(){   return $this->key + 1;   }   public function valid(){     return $this->key < $this->count;   } } foreach (new Number(5) as $key => $value){   echo "{$key} - {$value}\n"; } ``` 這個例子將輸出 ? ??1 - 0 ? ??2 - 2 ? ??3 - 4 ? ??4 - 6 ? ? 5 - 8 關于上面的number對象,被遍歷的過程。如果是初學者,可能會出現有點懵的情況。為了深入的了解Number對象被遍歷的時候內部是怎么工作的,我將代碼改了一下,將接口內的每個方法都盡心輸出,借此來窺探一下遍歷時對象內部方法的的執行情況。 ``` class Number implements Iterator{ protected $i = 1; protected $key; protected $val; protected $count; public function __construct(int $count){ $this->count = $count; echo "第{$this->i}步:對象初始化.\n"; $this->i++; } public function rewind(){ $this->key = 0; $this->val = 0; echo "第{$this->i}步:rewind()被調用.\n"; $this->i++; } public function next(){ $this->key += 1; $this->val += 2; echo "第{$this->i}步:next()被調用.\n"; $this->i++; } public function current(){ echo "第{$this->i}步:current()被調用.\n"; $this->i++; return $this->val; } public function key(){ echo "第{$this->i}步:key()被調用.\n"; $this->i++; return $this->key; } public function valid(){ echo "第{$this->i}步:valid()被調用.\n"; $this->i++; return $this->key < $this->count; } } $number = new Number(5); echo "start...\n"; foreach ($number as $key => $value){ echo "{$key} - {$value}\n"; } echo "...end...\n"; ``` 第1步:對象初始化. start... 第2步:rewind()被調用. 第3步:valid()被調用. 第4步:current()被調用. 第5步:key()被調用. 0 - 0 第6步:next()被調用. 第7步:valid()被調用. 第8步:current()被調用. 第9步:key()被調用. 1 - 2 第10步:next()被調用. 第11步:valid()被調用. 第12步:current()被調用. 第13步:key()被調用. 2 - 4 第14步:next()被調用. 第15步:valid()被調用. 第16步:current()被調用. 第17步:key()被調用. 3 - 6 第18步:next()被調用. 第19步:valid()被調用. 第20步:current()被調用. 第21步:key()被調用. 4 - 8 第22步:next()被調用. 第23步:valid()被調用. ...end... 那么這個跟yield有什么關系呢,這便是我們接下來要說的重點了。首先給大家介紹一下我總結出來的 yield 的特性,包含以下幾點。 **1.yield只能用于函數內部,在非函數內部運用會拋出錯誤** **2.如果函數包含了yield關鍵字的,那么函數執行后的返回值永遠都是一個Generator對象。** **3.如果函數內部同事包含yield和return 該函數的返回值依然是Generator對象,但是在生成Generator對象時,return語句后的代碼被忽略。** **4.Generator類實現了Iterator接口** **5.可以通過返回的Generator對象內部的方法,獲取到函數內部yield后面表達式的值** **6.可以通過Generator的send方法給yield 關鍵字賦一個值**。 **7.一旦返回的Generator對象被遍歷完成,便不能調用他的rewind方法來重置** **8.Generator對象不能被clone關鍵字克隆** **9.如果想要return 可以調用Generator的getReturn()方法。** 首先看第1點,可以明白我們文章開頭的gen函數執行后返回的是一個Generatory對象,所以代碼可以繼續執行下去輸出hello, world!,因此$gen是一個Generator對象,由于其實現了Iterator,所以這個對象可以被foreach語句遍歷。下面我們來看看對其進行遍歷,會是什么樣的效果。為了防止被死循環,我加多了一個break語句只進行十次循環,方便我們了解yield的一些特性。
                  <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>

                              哎呀哎呀视频在线观看