Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 10694

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Iteração de Objetos</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Classes e Objetos (PHP 5)"
HREF="language.oop5.html"><LINK
REL="PREVIOUS"
TITLE="Sobrecarga"
HREF="language.oop5.overloading.html"><LINK
REL="NEXT"
TITLE="Patterns"
HREF="language.oop5.patterns.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.oop5.overloading.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Capítulo 19. Classes e Objetos (PHP 5)</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.oop5.patterns.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop5.iterations"
>Iteração de Objetos</A
></H1
><P
>&#13;
   PHP 5 fornece uma maneira de defini objetos para que seja possível
   iterar por uma lista de items, com, por exeplo, uma instrução <A
HREF="control-structures.foreach.html"
>a seção <I
><TT
CLASS="literal"
>foreach</TT
></I
>  Capítulo 16</A
> . Por padrão, todas as
   propriedades <A
HREF="language.oop5.visibility.html"
>visíveis</A
> serão
   usadas para a iteração.

  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5833"
></A
><P
><B
>Exemplo 19-21. Simple Object Iteration</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">MyClass<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $var1 </font><font color="#007700">= </font><font color="#DD0000">'value 1'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $var2 </font><font color="#007700">= </font><font color="#DD0000">'value 2'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $var3 </font><font color="#007700">= </font><font color="#DD0000">'value 3'</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">protected $protected </font><font color="#007700">= </font><font color="#DD0000">'protected var'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">private&nbsp;&nbsp;&nbsp;$private&nbsp;&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">'private var'</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">iterateVisible</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"MyClass::iterateVisible:\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(</font><font color="#0000BB">$this </font><font color="#007700">as </font><font color="#0000BB">$key </font><font color="#007700">=&gt; </font><font color="#0000BB">$value</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"$key =&gt; $value</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$class </font><font color="#007700">= new </font><font color="#0000BB">MyClass</font><font color="#007700">();<br /><br />foreach(</font><font color="#0000BB">$class </font><font color="#007700">as </font><font color="#0000BB">$key </font><font color="#007700">=&gt; </font><font color="#0000BB">$value</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"$key =&gt; $value</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br />echo </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /><br /></font><font color="#0000BB">$class</font><font color="#007700">-&gt;</font><font color="#0000BB">iterateVisible</font><font color="#007700">();<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>O exemplo acima irá imprimir:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
var1 =&gt; value 1<br />var2 =&gt; value 2<br />var3 =&gt; value 3<br /><br />MyClass::iterateVisible:<br />var1 =&gt; value 1<br />var2 =&gt; value 2<br />var3 =&gt; value 3<br />protected =&gt; protected var<br />private =&gt; private var</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;  Como a saída mostra, o <A
HREF="control-structures.foreach.html"
>foreach</A
> iteragiu por cada uma
  das variáveis <A
HREF="language.oop5.visibility.html"
>visíveis</A
> que podem ser
  acessadas. Indo um pouco mais longe, você pode
  <CODE
CLASS="varname"
>implementar</CODE
> uma das <A
HREF="language.oop5.interfaces.html"
>interfaces</A
> internas do PHP5 chamada
  <CODE
CLASS="varname"
>Iterator</CODE
>. Isso permite que o objeto decida o que
  e como o objeto será iterado.
 </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5844"
></A
><P
><B
>Exemplo 19-22. Iteração de Objeto implmentando Iterator</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">MyIterator implements Iterator<br /></font><font color="#007700">{<br />&nbsp;&nbsp;</font><font color="#0000BB">private $var </font><font color="#007700">= array();<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__construct</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">) ) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">var </font><font color="#007700">= </font><font color="#0000BB">$array</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">rewind</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"rewinding\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">reset</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">var</font><font color="#007700">);<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">current</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$var </font><font color="#007700">= </font><font color="#0000BB">current</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">var</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"current: $var</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$var</font><font color="#007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">key</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$var </font><font color="#007700">= </font><font color="#0000BB">key</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">var</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"key: $var</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$var</font><font color="#007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">next</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$var </font><font color="#007700">= </font><font color="#0000BB">next</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">var</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"next: $var</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$var</font><font color="#007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">valid</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$var </font><font color="#007700">= </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">current</font><font color="#007700">() !== </font><font color="#0000BB">false</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"valid: </font><font color="#007700">{</font><font color="#DD0000">$var</font><font color="#007700">}\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$var</font><font color="#007700">;<br />&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$values </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">,</font><font color="#0000BB">2</font><font color="#007700">,</font><font color="#0000BB">3</font><font color="#007700">);<br /></font><font color="#0000BB">$it </font><font color="#007700">= new </font><font color="#0000BB">MyIterator</font><font color="#007700">(</font><font color="#0000BB">$values</font><font color="#007700">);<br /><br />foreach (</font><font color="#0000BB">$it </font><font color="#007700">as </font><font color="#0000BB">$a </font><font color="#007700">=&gt; </font><font color="#0000BB">$b</font><font color="#007700">) {<br />&nbsp;&nbsp;print </font><font color="#DD0000">"$a: $b</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>O exemplo acima irá imprimir:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
rewinding<br />current: 1<br />valid: 1<br />current: 1<br />key: 0<br />0: 1<br />next: 2<br />current: 2<br />valid: 1<br />current: 2<br />key: 1<br />1: 2<br />next: 3<br />current: 3<br />valid: 1<br />current: 3<br />key: 2<br />2: 3<br />next:<br />current:<br />valid:</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;   Você também pode definir sua classe para que ela não tenha que definir
   todas as funções do <CODE
CLASS="varname"
>Iterator</CODE
> simplesmente implementado
   a interface <CODE
CLASS="varname"
>IteratorAggregate</CODE
> do PHP 5.
  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5852"
></A
><P
><B
>Exemplo 19-23. Iteração de Objeto implementado IteratorAggregate</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">MyCollection implements IteratorAggregate </font><font color="#007700">{<br />&nbsp;&nbsp;</font><font color="#0000BB">private $items </font><font color="#007700">= array();<br />&nbsp;&nbsp;</font><font color="#0000BB">private $count </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /><br />&nbsp;&nbsp;</font><font color="#FF8000">/* Definião requirida da interface IteratorAggregate */<br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">getIterator</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;return new </font><font color="#0000BB">MyIterator</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">items</font><font color="#007700">);<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">add</font><font color="#007700">(</font><font color="#0000BB">$value</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">count</font><font color="#007700">++] = </font><font color="#0000BB">$value</font><font color="#007700">;<br />&nbsp;&nbsp;}<br /><br />}<br /><br /></font><font color="#0000BB">$coll </font><font color="#007700">= new </font><font color="#0000BB">MyCollection</font><font color="#007700">();<br /></font><font color="#0000BB">$coll</font><font color="#007700">-&gt;</font><font color="#0000BB">add</font><font color="#007700">(</font><font color="#DD0000">'value 1'</font><font color="#007700">);<br /></font><font color="#0000BB">$coll</font><font color="#007700">-&gt;</font><font color="#0000BB">add</font><font color="#007700">(</font><font color="#DD0000">'value 2'</font><font color="#007700">);<br /></font><font color="#0000BB">$coll</font><font color="#007700">-&gt;</font><font color="#0000BB">add</font><font color="#007700">(</font><font color="#DD0000">'value 3'</font><font color="#007700">);<br /><br />foreach (</font><font color="#0000BB">$coll </font><font color="#007700">as </font><font color="#0000BB">$key </font><font color="#007700">=&gt; </font><font color="#0000BB">$val</font><font color="#007700">) {<br />&nbsp;&nbsp;echo </font><font color="#DD0000">"key/value: </font><font color="#007700">[</font><font color="#DD0000">$key -&gt; $val</font><font color="#007700">]\n\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>O exemplo acima irá imprimir:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
rewinding<br />current: value 1<br />valid: 1<br />current: value 1<br />key: 0<br />key/value: [0 -&gt; value 1]<br /><br />next: value 2<br />current: value 2<br />valid: 1<br />current: value 2<br />key: 1<br />key/value: [1 -&gt; value 2]<br /><br />next: value 3<br />current: value 3<br />valid: 1<br />current: value 3<br />key: 2<br />key/value: [2 -&gt; value 3]<br /><br />next:<br />current:<br />valid:</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
    Para mais exemplos de iteração, veja a
    <A
HREF="ref.spl.html"
>Extensão SPL</A
>.
   </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.oop5.overloading.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.oop5.patterns.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Sobrecarga</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop5.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Patterns</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>