程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP設計模式漫談之迭代器模式(5)

PHP設計模式漫談之迭代器模式(5)

編輯:關於PHP編程

注意,迭代器是比對象集更好的抽象,因為我們可以讓InfiniteIterators,NoRewindIterators等,不用與普通數組陣列與一致,因此,Iterator缺少count()函數等功能。
在PHP官方手冊中可以找到完整的SPL迭代器列表。得益於對PHP的強力支持,使用迭代器模式的大部分工作都包括在標准實現中,下面的代碼示例就利用了標准Iterator和RecursiveIterators的功能。

___FCKpd___0___FCKpd___1

// lets play with RecursiveIterator implementations  $it = new RecursiveArrayIterator(array(      A,      B,      array(          C,          D      ),      array(          array(              E,              F          ),          array(              G,              H,              I          )      )  ));  // $it is a RecursiveIterator but also an Iterator,  // so it loops normally over the four elements  // of the array.  echo "Foreach over a RecursiveIterator: ";  foreach ($it as $value) {      echo $value;      // but RecursiveIterators specify additional      // methods to explore children nodes      $children = $it->hasChildren() ? {Yes} : {No};      echo $children, ;  }  echo " ";  // we can bridge it to a different contract via  // a RecursiveIteratorIterator, whose cryptic name  // should be read as an Iterator that spans over  // a RecursiveIterator.  echo "Foreach over a RecursiveIteratorIterator: ";  foreach (new RecursiveIteratorIterator($it) as $value) {      echo $value;  }  echo " "; 原文名:Practical Php Patterns: Iterator        作者:Giorgio

content; 
    private $_index = 0; 
 
    public function __construct(array $content) 
    { 
        $this->_content = $content; 
    } 
 
    public function rewind() 
    { 
        $this->_index = 0; 
    } 
 
    public function valid() 
    { 
        return isset($this->_content[$this->_index]); 
    } 
 
    public function current() 
    { 
        return $this->_content[$this->_index]; 
    } 
 
    public function key() 
    { 
        return $this->_index; 
    } 
 
    public function next() 
    { 
        $this->_index++; 
    } 

 
$arrayarray = array(A, B, C, D); 
echo "Collection: "; 
foreach (new Collection($array) as $key => $value) { 
    echo "$key => $value. "; 

echo " ";
___FCKpd___1___FCKpd___2原文名:Practical Php Patterns: Iterator        作者:Giorgio

content; 
 
    public function __construct(array $content) 
    { 
        $this->_content = $content; 
    } 
 
    public function contains($number) 
    { 
        return in_array($number, $this->_content); 
    } 
 
    /** 
     * Only this method is necessary to implement IteratorAggregate. 
     * @return Iterator 
     */ 
    public function getIterator() 
    { 
        return new ArrayIterator($this->_content); 
    } 

 
echo "NumbersSet: "; 
foreach (new NumbersSet($array) as $key => $value) { 
    echo "$key => $value. "; 

echo " ";
___FCKpd___2原文名:Practical Php Patterns: Iterator        作者:Giorgio

content; 
    private $_index = 0; 
 
    public function __construct(array $content) 
    { 
        $this->_content = $content; 
    } 
 
    public function rewind() 
    { 
        $this->_index = 0; 
    } 
 
    public function valid() 
    { 
        return isset($this->_content[$this->_index]); 
    } 
 
    public function current() 
    { 
        return $this->_content[$this->_index]; 
    } 
 
    public function key() 
    { 
        return $this->_index; 
    } 
 
    public function next() 
    { 
        $this->_index++; 
    } 

 
$arrayarray = array(A, B, C, D); 
echo "Collection: "; 
foreach (new Collection($array) as $key => $value) { 
    echo "$key => $value. "; 

echo " ";
___FCKpd___1___FCKpd___2

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved