Sophie

Sophie

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

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
>Referências dentro do construtor</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 4)"
HREF="language.oop.html"><LINK
REL="PREVIOUS"
TITLE="As funções mágicas __sleep e __wakeup"
HREF="language.oop.magic-functions.html"><LINK
REL="NEXT"
TITLE="Comparando Objetos"
HREF="language.oop.object-comparison.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.oop.magic-functions.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Capítulo 18. Classes e Objetos(PHP 4)</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.oop.object-comparison.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop.newref"
>Referências dentro do construtor</A
></H1
><P
>&#13;    A criação de referências em construtores pode gerar resultados
    confusos. Esta seção tentará ajudá-lo e evitar essas situações.

    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5518"
></A
><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">Foo </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">Foo</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// cria uma referencia dentro do array global $globalref<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">global </font><font color="#0000BB">$globalref</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$globalref</font><font color="#007700">[] = &amp;</font><font color="#0000BB">$this</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// configura o nome conforme o parametro<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">setName</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// e o mostra<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">echoName</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;br /&gt;"</font><font color="#007700">,</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">name</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">setName</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">name </font><font color="#007700">= </font><font color="#0000BB">$name</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
  </P
><P
>&#13;    Vamos verificar, abaixo, se há alguma diferença entre
    <CODE
CLASS="varname"
>$bar1</CODE
>, que foi criado usando
    operador de cópia <TT
CLASS="literal"
>=</TT
>, e
    <CODE
CLASS="varname"
>$bar2</CODE
> que foi criado usando
    o operador de referência <TT
CLASS="literal"
>=&#38;</TT
> ...

    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5525"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$bar1 </font><font color="#007700">= new </font><font color="#0000BB">Foo</font><font color="#007700">(</font><font color="#DD0000">'configurado no construtor'</font><font color="#007700">);<br /></font><font color="#0000BB">$bar1</font><font color="#007700">-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /></font><font color="#0000BB">$globalref</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* saida:<br />configurado no construtor<br />configurado no construtor<br />configurado no construtor */<br /><br /></font><font color="#0000BB">$bar2 </font><font color="#007700">=&amp; new </font><font color="#0000BB">Foo</font><font color="#007700">(</font><font color="#DD0000">'configurado no construtor'</font><font color="#007700">);<br /></font><font color="#0000BB">$bar2</font><font color="#007700">-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /></font><font color="#0000BB">$globalref</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* saida:<br />configurado no construtor<br />configurado no construtor<br />configurado no construtor */<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    Aparentemente não há nenhuma diferença, mas de fato há uma
    muito significativa: <CODE
CLASS="varname"
>$bar1</CODE
> e
    <CODE
CLASS="varname"
>$globalref[0]</CODE
> não se referenciam, elas
    NÃO são a mesma variável. Isto acontece porque "new" não
    retorna uma referência por default. Ao invés, retorna uma cópia.
    <DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
      Isto não causa perda de performance (desde que o PHP 4 usa a contagem
      de referências) retornando copias em vez de referências. Do contrário,
      isso oferece melhora por simplificar o trabalho com cópias
      ao invés de referências, porque a criação de referências toma mais
      tempo enquanto a criação de cópias virtualmente não toma tempo algum (a não ser
      no caso de grandes arrays ou objetos, onde um deles é modificado
      e o(s) outro(s) também na seqüência, então é melhor
      usar referências para mudar todos ao mesmo tempo).
     </P
></BLOCKQUOTE
></DIV
>
    Para comprovar o que escrevemos acima, analise o seguinte código.

    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5532"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Agora nos vamos mudar o nome. O que voce espera?<br />// Voce pode acreditar que ambos $bar1 e $globalref[0] mudem seus nomes...<br /></font><font color="#0000BB">$bar1</font><font color="#007700">-&gt;</font><font color="#0000BB">setName</font><font color="#007700">(</font><font color="#DD0000">'configurado por fora'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Como mencionado, este nao eh o caso.<br /></font><font color="#0000BB">$bar1</font><font color="#007700">-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /></font><font color="#0000BB">$globalref</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* output:<br />configurado por fora<br />configurado no construtor */<br /><br />// Agora vamos ver a diferenca entre $bar2 e $globalref[1]<br /></font><font color="#0000BB">$bar2</font><font color="#007700">-&gt;</font><font color="#0000BB">setName</font><font color="#007700">(</font><font color="#DD0000">'configurado por fora'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Por sorte, eles nao sao apenas iguais, eles sao a mesma variavel<br />// Assim, $bar2-&gt;name e $globalref[1]-&gt;name sao o mesmo tambem<br /></font><font color="#0000BB">$bar2</font><font color="#007700">-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /></font><font color="#0000BB">$globalref</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]-&gt;</font><font color="#0000BB">echoName</font><font color="#007700">();<br /><br /></font><font color="#FF8000">/* output:<br />configurado por fora<br />configurado por fora */<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;   E apenas mais um exemplo final. Entenda-o com cuidado.

    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5535"
></A
><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">A </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">A</font><font color="#007700">(</font><font color="#0000BB">$i</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">value </font><font color="#007700">= </font><font color="#0000BB">$i</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// tente entender porque aqui nos nao precisamos de referencia<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">b </font><font color="#007700">= new </font><font color="#0000BB">B</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">createRef</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">c </font><font color="#007700">= new </font><font color="#0000BB">B</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">echoValue</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;br /&gt;"</font><font color="#007700">,</font><font color="#DD0000">"classe "</font><font color="#007700">,</font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">),</font><font color="#DD0000">': '</font><font color="#007700">,</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">value</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /><br />class </font><font color="#0000BB">B </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">B</font><font color="#007700">(&amp;</font><font color="#0000BB">$a</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">a </font><font color="#007700">= &amp;</font><font color="#0000BB">$a</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">echoValue</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"&lt;br /&gt;"</font><font color="#007700">,</font><font color="#DD0000">"classe "</font><font color="#007700">,</font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">),</font><font color="#DD0000">': '</font><font color="#007700">,</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">a</font><font color="#007700">-&gt;</font><font color="#0000BB">value</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">// Tente entender porque usando uma simples copia aqui ter<br />// um resultado indesejavel na linha marcada com *<br /></font><font color="#0000BB">$a </font><font color="#007700">=&amp; new </font><font color="#0000BB">A</font><font color="#007700">(</font><font color="#0000BB">10</font><font color="#007700">);<br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">createRef</font><font color="#007700">();<br /><br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">echoValue</font><font color="#007700">();<br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">b</font><font color="#007700">-&gt;</font><font color="#0000BB">echoValue</font><font color="#007700">();<br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">c</font><font color="#007700">-&gt;</font><font color="#0000BB">echoValue</font><font color="#007700">();<br /><br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">value </font><font color="#007700">= </font><font color="#0000BB">11</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">echoValue</font><font color="#007700">();<br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">b</font><font color="#007700">-&gt;</font><font color="#0000BB">echoValue</font><font color="#007700">(); </font><font color="#FF8000">// *<br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">c</font><font color="#007700">-&gt;</font><font color="#0000BB">echoValue</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
><PRE
CLASS="screen"
>classe A: 10
classe B: 10
classe B: 10
classe A: 11
classe B: 11
classe B: 11</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </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.oop.magic-functions.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.oop.object-comparison.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>As funções mágicas <TT
CLASS="literal"
>__sleep</TT
> e <TT
CLASS="literal"
>__wakeup</TT
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Comparando Objetos</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>