预定义接口-迭代器Iterator_PHP教程

WBOY
Lepaskan: 2016-07-13 10:29:48
asal
809 orang telah melayarinya

<?<span>php
</span><span>/*</span><span>
可在内部迭代自己的外部迭代器或类的接口。
Iterator  extends Traversable  {
abstract public mixed current  ( void )
abstract public scalar key  ( void )
abstract public void next  ( void )
abstract public void rewind  ( void )
abstract public boolean valid  ( void )
}
</span><span>*/</span>
<span>class</span>  myIterator  <span>implements</span><span>  Iterator{
    </span><span>private</span>  <span>$position</span>  =  0<span> ;
    </span><span>private</span>  <span>$array</span>  = <span>array</span><span>(
         </span>"firstelement" ,
         "secondelement" ,
         "lastelement" ,<span>
    );  

    </span><span>public</span> <span>function</span><span>  __construct () {
         </span><span>$this</span> -> position  =  0<span> ;
    }

    </span><span>function</span>  <span>rewind</span><span> () {
         </span><span>var_dump</span> ( <span>__METHOD__</span><span> );
         </span><span>$this</span> -> position  =  0<span> ;
    }

    </span><span>function</span>  <span>current</span><span> () {
         </span><span>var_dump</span> ( <span>__METHOD__</span><span> );
        </span><span>return</span>  <span>$this</span> -> <span>array</span> [ <span>$this</span> -><span> position ];
    }

    </span><span>function</span>  <span>key</span><span> () {
         </span><span>var_dump</span> ( <span>__METHOD__</span><span> );
         </span><span>return</span>  <span>$this</span> -><span> position ;
    }

    </span><span>function</span>  <span>next</span><span> () {
         </span><span>var_dump</span> ( <span>__METHOD__</span><span> );
        </span>++ <span>$this</span> -><span> position ;
        
    }

    </span><span>function</span><span>  valid () {
         </span><span>var_dump</span> ( <span>__METHOD__</span><span> );
        </span><span>return</span> <span>isset</span>( <span>$this</span> -> <span>array</span> [ <span>$this</span> -><span> position ]);
    }
}

 </span><span>$it</span>  = <span>new</span><span>  myIterator ;

</span><span>foreach</span>( <span>$it</span>  <span>as</span>  <span>$key</span>  =>  <span>$value</span><span> ) {
     </span><span>print_r</span>( <span>$key</span>.'/'.<span>$value</span><span> );
     </span><span>echo</span>  "<br />"<span> ;
}

</span><span>/*</span><span>
foreach循环第一次执行前,先调用rewind,返回到迭代器的第一个元素,开始位置也可控制
第二步执行valid检查当前位置是否有效,有效继续往下执行,无效则循环停止
第三步执行current返回当前元素的值
第四步执行key 返回当前元素的键
第五步:第一次循环结束后执行next向前移动到下一个元素开始下一轮的循环(紧接着跳过rewind,进行valid检测当前位置)
往下next,valid,current,key循环依次执行,直到valid到无效的位置
</span><span>*/</span>
Salin selepas log masuk

内容是自己总结的,有不对的地方,希望能指出!

更多内容后续补充...

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/770158.htmlTechArticle? php /* 可在内部迭代自己的外部迭代器或类的接口。Iterator extends Traversable {abstract public mixed current ( void )abstract public scalar key ( void )abstract...
Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan