看看PHP迭代器的內部執行過程

來源:互聯網
上載者:User

標籤:span   遍曆   class   imp   ext   工作流程   ons   執行   需要   

 1 class myIterator implements Iterator { 2     private $position = 0; 3     private $array = array( 4         "first_element", 5         "second_element", 6         "last_element", 7     );   8  9     public function __construct() {10         $this->position = 0;11     }12 13     function rewind() {14         var_dump(__METHOD__);15         $this->position = 0;16     }17 18     function current() {19         var_dump(__METHOD__);20         return $this->array[$this->position];21     }22 23     function key() {24         var_dump(__METHOD__);25         return $this->position;26     }27 28     function next() {29         var_dump(__METHOD__);30         ++$this->position;31     }32 33     function valid() {34         var_dump(__METHOD__);35         return isset($this->array[$this->position]);36     }37 }38 39 $it = new myIterator;40 41 foreach($it as $key => $value) {42     echo ‘輸出索引值:‘;43     var_dump($key, $value);44     //echo $key;45     echo "\n";46 }

程式運行輸出:

string(18) "myIterator::rewind"string(17) "myIterator::valid"string(19) "myIterator::current"string(15) "myIterator::key"輸出索引值:int(0)string(13) "first_element"string(16) "myIterator::next"string(17) "myIterator::valid"string(19) "myIterator::current"string(15) "myIterator::key"輸出索引值:int(1)string(14) "second_element"string(16) "myIterator::next"string(17) "myIterator::valid"string(19) "myIterator::current"string(15) "myIterator::key"輸出索引值:int(2)string(12) "last_element"string(16) "myIterator::next"string(17) "myIterator::valid"

一般的迭代器內部需要下面的方法:

  • Iterator::current — Return the current element 返回當前元素
  • Iterator::key — Return the key of the current element 返回當前元素的鍵
  • Iterator::next — Move forward to next element 移向下一個元素
  • Iterator::rewind — Rewind the Iterator to the first element 重新回到第一個元素
  • Iterator::valid — Checks if current position is valid 檢查當前位置的有效性

如果不是很清楚迭代器的內容工作流程,可以查看下面的迭代器對數組的遍曆過程:

/*** */class MyIterator implements Iterator{     private $var = array();     public function __construct($array)     {         if (is_array($array)) {            $this->var = $array;         }     }     public function rewind() {         echo "倒回第一個元素\n";        reset($this->var);     }     public function current() {        $var = current($this->var);         echo "當前元素: $var\n";         return $var;     }     public function key() {        $var = key($this->var);         echo "當前元素的鍵: $var\n";         return $var;     }     public function next() {        $var = next($this->var);         echo "移向下一個元素: $var\n";         return $var;     }     public function valid() {        $var = $this->current() !== false;         echo "檢查有效性: {$var}\n";         return $var;     }}$values = array(1,2,3);$it = new MyIterator($values);foreach ($it as $k => $v) {     print "此時索引值對 -- key $k: value $v\n\n";}

程式運行結果:

倒回第一個元素當前元素: 1檢查有效性: 1當前元素: 1當前元素的鍵: 0此時索引值對 -- key 0: value 1移向下一個元素: 2當前元素: 2檢查有效性: 1當前元素: 2當前元素的鍵: 1此時索引值對 -- key 1: value 2移向下一個元素: 3當前元素: 3檢查有效性: 1當前元素: 3當前元素的鍵: 2此時索引值對 -- key 2: value 3移向下一個元素: 當前元素: 檢查有效性: 

看看PHP迭代器的內部執行過程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.