<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/reserved.interfaces.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'class.iterator.php',
    1 => 'Iterator',
    2 => 'L\'interface Iterator',
  ),
  'up' => 
  array (
    0 => 'reserved.interfaces.php',
    1 => 'Interfaces et Classes Pr&eacute;d&eacute;finies',
  ),
  'prev' => 
  array (
    0 => 'class.traversable.php',
    1 => 'Traversable',
  ),
  'next' => 
  array (
    0 => 'iterator.current.php',
    1 => 'Iterator::current',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/predefined/iterator.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/class.iterator.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="class.iterator" class="reference">

 <h1 class="title">L&#039;interface Iterator</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5, PHP 7, PHP 8)</p>


  <div class="section" id="iterator.intro">
   <h2 class="title">Introduction</h2>
   <p class="para">
    Interface pour les itérateurs ou les objets externes qui peuvent
    être itérés eux-mêmes en interne.
   </p>
  </div>


  <div class="section" id="iterator.synopsis">
   <h2 class="title">Sommaire de l&#039;Interface</h2>


   <div class="classsynopsis"><div class="classsynopsisinfo">
    <span class="modifier">interface</span> <strong class="interfacename"><strong class="interfacename">Iterator</strong></strong>

    <span class="modifier">extends</span>
      <a href="class.traversable.php" class="interfacename">Traversable</a> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Méthodes */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="iterator.current.php" class="methodname">current</a></span>(): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iterator.key.php" class="methodname">key</a></span>(): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iterator.next.php" class="methodname">next</a></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iterator.rewind.php" class="methodname">rewind</a></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="iterator.valid.php" class="methodname">valid</a></span>(): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

   }</div>


  </div>
  
  <div class="section" id="iterator.iterators">
   <h2 class="title">Itérateurs prédéfinis</h2>
   <p class="para">
    PHP fournit quelques itérateurs pour certaines tâches.
    Voir les <a href="spl.iterators.php" class="link">itérateurs SPL</a> pour une liste complète.
   </p>
  </div>

  <div class="section" id="iterator.examples">
   <h2 class="title">Exemples</h2>
   <div class="example" id="iterator.example.basic">
    <p><strong>Exemple #1 Exemple simple</strong></p>
    <div class="example-contents"><p>
     Cet exemple montre l&#039;ordre d&#039;appel des méthodes lors d&#039;un appel
     à l&#039;instruction <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> sur un itérateur.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">"premierelement"</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">"secondelement"</span><span style="color: #007700">,<br />        </span><span style="color: #DD0000">"dernierelement"</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">-&gt;</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 />    public function </span><span style="color: #0000BB">rewind</span><span style="color: #007700">(): </span><span style="color: #0000BB">void </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">-&gt;</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 />    #[</span><span style="color: #0000BB">\ReturnTypeWillChange</span><span style="color: #007700">]<br />    public 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">-&gt;</span><span style="color: #0000BB">array</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">position</span><span style="color: #007700">];<br />    }<br /><br />    #[</span><span style="color: #0000BB">\ReturnTypeWillChange</span><span style="color: #007700">]<br />    public 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">-&gt;</span><span style="color: #0000BB">position</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">next</span><span style="color: #007700">(): </span><span style="color: #0000BB">void </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">-&gt;</span><span style="color: #0000BB">position</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">valid</span><span style="color: #007700">(): </span><span style="color: #0000BB">bool </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">-&gt;</span><span style="color: #0000BB">array</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</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">=&gt; </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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus est similaire à :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(18) &quot;myIterator::rewind&quot;
string(17) &quot;myIterator::valid&quot;
string(19) &quot;myIterator::current&quot;
string(15) &quot;myIterator::key&quot;
int(0)
string(14) &quot;premierelement&quot;

string(16) &quot;myIterator::next&quot;
string(17) &quot;myIterator::valid&quot;
string(19) &quot;myIterator::current&quot;
string(15) &quot;myIterator::key&quot;
int(1)
string(13) &quot;secondelement&quot;

string(16) &quot;myIterator::next&quot;
string(17) &quot;myIterator::valid&quot;
string(19) &quot;myIterator::current&quot;
string(15) &quot;myIterator::key&quot;
int(2)
string(14) &quot;dernierelement&quot;

string(16) &quot;myIterator::next&quot;
string(17) &quot;myIterator::valid&quot;
</pre></div>
    </div>
   </div>
  </div>

  <div class="section" id="iterator.seealso">
   <h2 class="title">Voir aussi</h2>
   <p class="para">Voir aussi l&#039;<a href="language.oop5.iterations.php" class="link">itération d&#039;objet</a>.</p>
  </div>

 </div>

 





 





 





 





 






<h2>Sommaire</h2><ul class="chunklist chunklist_reference"><li><a href="iterator.current.php">Iterator::current</a> — Retourne l'&eacute;l&eacute;ment courant</li><li><a href="iterator.key.php">Iterator::key</a> — Retourne la cl&eacute; de l'&eacute;l&eacute;ment courant</li><li><a href="iterator.next.php">Iterator::next</a> — Se d&eacute;place sur l'&eacute;l&eacute;ment suivant</li><li><a href="iterator.rewind.php">Iterator::rewind</a> — Replace l'it&eacute;rateur sur le premier &eacute;l&eacute;ment</li><li><a href="iterator.valid.php">Iterator::valid</a> — V&eacute;rifie si la position courante est valide</li></ul>
</div>
<?php manual_footer($setup); ?>