<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>The Iterator interface</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.traversable.html">Traversable</a></div> <div class="next" style="text-align: right; float: right;"><a href="iterator.current.html">Iterator::current</a></div> <div class="up"><a href="reserved.interfaces.html">Predefined Interfaces and Classes</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="class.iterator" class="reference"> <h1 class="title">The Iterator interface</h1> <div class="partintro"><p class="verinfo">(PHP 5 >= 5.0.0)</p> <div class="section" id="iterator.intro"> <h2 class="title">Introduction</h2> <p class="para"> Interface for external iterators or objects that can be iterated themselves internally. </p> </div> <div class="section" id="iterator.synopsis"> <h2 class="title">Interface synopsis</h2> <div class="classsynopsis"> <div class="ooclass"></div> <div class="classsynopsisinfo"> <span class="ooclass"> <strong class="classname">Iterator</strong> </span> <span class="ooclass"> <span class="modifier">extends</span> <a href="class.traversable.html" class="classname">Traversable</a> </span> {</div> <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type">mixed</span> <span class="methodname"><a href="iterator.current.html" class="methodname">current</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type">scalar</span> <span class="methodname"><a href="iterator.key.html" class="methodname">key</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type">void</span> <span class="methodname"><a href="iterator.next.html" class="methodname">next</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type">void</span> <span class="methodname"><a href="iterator.rewind.html" class="methodname">rewind</a></span> ( <span class="methodparam">void</span> )</div> <div class="methodsynopsis dc-description"> <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type">boolean</span> <span class="methodname"><a href="iterator.valid.html" class="methodname">valid</a></span> ( <span class="methodparam">void</span> )</div> }</div> </div> <div class="section" id="iterator.iterators"> <h2 class="title">Predefined iterators</h2> <p class="para"> PHP already provides a number of iterators for many day to day tasks. See <a href="spl.iterators.html" class="link">SPL iterators</a> for a list. </p> </div> <div class="section" id="iterator.examples"> <h2 class="title">Examples</h2> <div class="example" id="iterator.example.basic"> <p><strong>Example #1 Basic usage</strong></p> <div class="example-contents"><p> This example demonstrates in which order methods are called when using <a href="control-structures.foreach.html" class="link">foreach</a> with an iterator. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">myIterator </span><span style="color: #007700">implements </span><span style="color: #0000BB">Iterator </span><span style="color: #007700">{<br /> private </span><span style="color: #0000BB">$position </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> private </span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">"firstelement"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"secondelement"</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"lastelement"</span><span style="color: #007700">,<br /> ); <br /><br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">rewind</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">current</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">array</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position</span><span style="color: #007700">];<br /> }<br /><br /> function </span><span style="color: #0000BB">key</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">next</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br /> ++</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">valid</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">);<br /> return isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">array</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">position</span><span style="color: #007700">]);<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$it </span><span style="color: #007700">= new </span><span style="color: #0000BB">myIterator</span><span style="color: #007700">;<br /><br />foreach(</span><span style="color: #0000BB">$it </span><span style="color: #007700">as </span><span style="color: #0000BB">$key </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> string(18) "myIterator::rewind" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(0) string(12) "firstelement" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(1) string(13) "secondelement" string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(2) string(11) "lastelement" string(16) "myIterator::next" string(17) "myIterator::valid" </pre></div> </div> </div> </div> </div> <h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="iterator.current.html">Iterator::current</a> — Return the current element</li><li><a href="iterator.key.html">Iterator::key</a> — Return the key of the current element</li><li><a href="iterator.next.html">Iterator::next</a> — Move forward to next element</li><li><a href="iterator.rewind.html">Iterator::rewind</a> — Rewind the Iterator to the first element</li><li><a href="iterator.valid.html">Iterator::valid</a> — Checks if current position is valid</li></ul> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.traversable.html">Traversable</a></div> <div class="next" style="text-align: right; float: right;"><a href="iterator.current.html">Iterator::current</a></div> <div class="up"><a href="reserved.interfaces.html">Predefined Interfaces and Classes</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>