Sophie

Sophie

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

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
>Comparando 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="Clonando objetos"
HREF="language.oop5.cloning.html"><LINK
REL="NEXT"
TITLE="Reflexão"
HREF="language.oop5.reflection.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.cloning.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.reflection.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.object-comparison"
>Comparando objetos</A
></H1
><P
>&#13;    No PHP 5, comparação de objetos é mais complicada que no PHP 4 e mais
    de acordo com o que é esperado de uma Linguagem Orientada a Objetos
    (não que PHP 5 seja uma delas).
   </P
><P
>&#13;    Quando usar o operador de comparação (<TT
CLASS="literal"
>==</TT
>),
    variáveis objeto são comparadas de maneira simples, nominalmente: Duas instâncias
    de objetos são iguais se tem os mesmos atributos e valores, e são
    instâncias da mesma classe.
   </P
><P
>&#13;    Por outro lado, quando usando o operador de identidade (<TT
CLASS="literal"
>===</TT
>),
    variáveis objetos são identicas se e somente se elas se referem a mesma
    instância da mesma classe.
   </P
><P
>&#13;    Um exemplo evidenciará essas regras.
    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5975"
></A
><P
><B
>Exemplo 19-32. Exemplo de comparação de objetos no PHP 5</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">function </font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$bool</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$bool </font><font color="#007700">=== </font><font color="#0000BB">false</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#DD0000">'FALSO'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#DD0000">'VERDADEIRO'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">compareObjetos</font><font color="#007700">(&amp;</font><font color="#0000BB">$o1</font><font color="#007700">, &amp;</font><font color="#0000BB">$o2</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 == o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">== </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 != o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">!= </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 === o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">=== </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'o1 !== o2 : '</font><font color="#007700">.</font><font color="#0000BB">bool2str</font><font color="#007700">(</font><font color="#0000BB">$o1 </font><font color="#007700">!== </font><font color="#0000BB">$o2</font><font color="#007700">).</font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /><br />class </font><font color="#0000BB">Flag<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$flag</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">Flag</font><font color="#007700">(</font><font color="#0000BB">$flag</font><font color="#007700">=</font><font color="#0000BB">true</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">flag </font><font color="#007700">= </font><font color="#0000BB">$flag</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">OutraFlag<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$flag</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">OutraFlag</font><font color="#007700">(</font><font color="#0000BB">$flag</font><font color="#007700">=</font><font color="#0000BB">true</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">flag </font><font color="#007700">= </font><font color="#0000BB">$flag</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$o </font><font color="#007700">= new </font><font color="#0000BB">Flag</font><font color="#007700">();<br /></font><font color="#0000BB">$p </font><font color="#007700">= new </font><font color="#0000BB">Flag</font><font color="#007700">();<br /></font><font color="#0000BB">$q </font><font color="#007700">= </font><font color="#0000BB">$o</font><font color="#007700">;<br /></font><font color="#0000BB">$r </font><font color="#007700">= new </font><font color="#0000BB">OutraFlag</font><font color="#007700">();<br /><br />echo </font><font color="#DD0000">"Duas instâncias da mesma classe\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjetos</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">, </font><font color="#0000BB">$p</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"\nDuas referências para a mesma instância\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjetos</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">, </font><font color="#0000BB">$q</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"\nInstâncias de duas classes diferentes\n"</font><font color="#007700">;<br /></font><font color="#0000BB">compareObjetos</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">, </font><font color="#0000BB">$r</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    This example will output:
    <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Duas instâncias da mesma classe
o1 == o2 : VERDADEIRO
o1 != o2 : FALSO
o1 === o2 : FALSO
o1 !== o2 : VERDADEIRO

Duas referências para a mesma instância
o1 == o2 : VERDADEIRO
o1 != o2 : FALSO
o1 === o2 : VERDADEIRO
o1 !== o2 : FALSO

Instâncias de duas classes diferentes
o1 == o2 : FALSO
o1 != o2 : VERDADEIRO
o1 === o2 : FALSO
o1 !== o2 : VERDADEIRO</PRE
></TD
></TR
></TABLE
>
   </P
></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.cloning.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.reflection.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Clonando objetos</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"
>Reflexão</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>