Home > Backend Development > PHP Tutorial > Predefined interface-Iterator_PHP tutorial

Predefined interface-Iterator_PHP tutorial

WBOY
Release: 2016-07-13 10:29:48
Original
820 people have browsed it

<?<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>
Copy after login

The content is summarized by myself. If there are any mistakes, I hope you can point them out!

More content will be added later...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/770158.htmlTechArticle? php /* Interface that can iterate internally through its own external iterator or class. Iterator extends Traversable {abstract public mixed current ( void )abstract public scalar key ( void )abstract...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template