Iteratorパターン

 class MyIterator {
    $pos  = 0;
    $keys = array();
    $vals = array();
    public function __construct($list) {
        $this->keys = array_keys($list);
        $this->vals = array_values($list);
    }
    public function hasNext() {
        return array_key_exists($this->pos + 1, $this->keys);
    }
    public function next() {
        if ($this->hasNext()) {
            $val = $this->vals[$this->pos];
            $this->pos += 1;
            return $val;
        }
    }
    public function rewind() {
        $this->pos = 0;
    }
    public function getArray() {
        return array_combine($this->keys, $this->vals);
    }

}

 class Foo {
     public function getIterator() {
         return new MyIetrator($this->array)
     }
 }
 $it = $foo->getIterator();
 while ($val = $it->next()) {
 }

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS