Sophie

Sophie

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

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
>Métodos Mágicos</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="Patterns"
HREF="language.oop5.patterns.html"><LINK
REL="NEXT"
TITLE="Palavra-Chave 'final'"
HREF="language.oop5.final.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.patterns.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.final.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.magic"
>Métodos Mágicos</A
></H1
><P
>&#13;   Os nomes de funções
   <TT
CLASS="literal"
>__construct</TT
>,
   <TT
CLASS="literal"
>__destruct</TT
>
   (veja <A
HREF="language.oop5.decon.html"
>Construtores e Destrutores</A
>),
   <TT
CLASS="literal"
>__call</TT
>,
   <TT
CLASS="literal"
>__get</TT
>,
   <TT
CLASS="literal"
>__set</TT
>,
   <TT
CLASS="literal"
>__isset</TT
>,
   <TT
CLASS="literal"
>__unset</TT
>
   (veja <A
HREF="language.oop5.overloading.html"
>Sobrecarga</A
>),
   <TT
CLASS="literal"
>__sleep</TT
>,
   <TT
CLASS="literal"
>__wakeup</TT
>,
   <TT
CLASS="literal"
>__toString</TT
>,
   <TT
CLASS="literal"
>__set_state</TT
>,
   <A
HREF="language.oop5.cloning.html"
>__clone</A
> and
   <A
HREF="language.oop5.autoload.html"
>__autoload</A
>
   são mágicos nas classes do PHP. Você
   não pode ter funções com esses nomes em nenhuma de suas
   classes a não ser que queria que a funcionalidade mágica associada
   com eles.
  </P
><DIV
CLASS="caution"
><P
></P
><TABLE
CLASS="caution"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Cuidado</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;    PHP reserva todas as funções com nomes começando com __ como mágicas.
    É recomendado que você não use funções com nomes com
    __ no PHP a não ser que você queira alguma funcionalidade mágica documentada.
   </P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.magic.sleep"
><TT
CLASS="literal"
>__sleep</TT
> and <TT
CLASS="literal"
>__wakeup</TT
></A
></H2
><P
>&#13;    <A
HREF="function.serialize.html"
><B
CLASS="function"
>serialize()</B
></A
> checa se sua classe tem uma função com
    o nome mágico <TT
CLASS="literal"
>__sleep</TT
>. Se tiver, a função é
    executa antes de qualqer serialização. Ela pode limpar o objeto
    e deve retornar um array com os nomes de todas as variáveis
    do objeto que devem ser serializadas.
   </P
><P
>&#13;    O intuito do método <TT
CLASS="literal"
>__sleep</TT
> é fechar qualquer
    conexão com bancos de dados que o objeto possa ter, enviando dados
    pendentes ou realizar tarefas similares de limpeza. Além disso, a função é
    útil se você tiver objetos muito grandes que não precisarão ser
    salvos completamente.
   </P
><P
>&#13;    Inversamente, <A
HREF="function.unserialize.html"
><B
CLASS="function"
>unserialize()</B
></A
> checa pela
    presença da função com o nome mágico
    <TT
CLASS="literal"
>__wakeup</TT
>. Se achar, essa função pode
    reconstruir qualquer recursos que o objeto pode ter.
   </P
><P
>&#13;    O intuito do método <TT
CLASS="literal"
>__wakeup</TT
> é
    reestabelecer qualquer conexão com banco de daos que podem ter sido perdidas
    durante a serialização e realizar tarefas de
    reinicialização.
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5917"
></A
><P
><B
>Exemplo 19-26. Sleep e wakeup</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">Connection </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">protected $link</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">private $server</font><font color="#007700">, </font><font color="#0000BB">$username</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">, </font><font color="#0000BB">$db</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&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">$server</font><font color="#007700">, </font><font color="#0000BB">$username</font><font color="#007700">, </font><font color="#0000BB">$password</font><font color="#007700">, </font><font color="#0000BB">$db</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">server </font><font color="#007700">= </font><font color="#0000BB">$server</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">username </font><font color="#007700">= </font><font color="#0000BB">$username</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">password </font><font color="#007700">= </font><font color="#0000BB">$password</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">db </font><font color="#007700">= </font><font color="#0000BB">$db</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">connect</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">private </font><font color="#007700">function </font><font color="#0000BB">connect</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">link </font><font color="#007700">= </font><font color="#0000BB">mysql_connect</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">server</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">username</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">password</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">db</font><font color="#007700">, </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">link</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__sleep</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mysql_close</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">link</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__wakeup</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">connect</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.magic.tostring"
><TT
CLASS="literal"
>__toString</TT
></A
></H2
><P
>&#13;    O método <TT
CLASS="literal"
>__toString</TT
> permite que uma classe decida
    como se comportar quando for convertida para uma string.
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5925"
></A
><P
><B
>Exemplo 19-27. Exemplo Simples</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Declara uma classe simples<br /></font><font color="#007700">class </font><font color="#0000BB">ClasseTeste<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $foo</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&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">$foo</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">foo </font><font color="#007700">= </font><font color="#0000BB">$foo</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">__toString</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">foo</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$classe </font><font color="#007700">= new </font><font color="#0000BB">ClasseTeste</font><font color="#007700">(</font><font color="#DD0000">'Olá'</font><font color="#007700">);<br />echo </font><font color="#0000BB">$classe</font><font color="#007700">;<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"
>Olá</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    Vale lembrar que o método <TT
CLASS="literal"
>__toString</TT
>
    só será chamado quando diretamente combinado com
    <A
HREF="function.echo.html"
><B
CLASS="function"
>echo()</B
></A
> ou <A
HREF="function.print.html"
><B
CLASS="function"
>print()</B
></A
>.
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5934"
></A
><P
><B
>Exemplo 19-28. Casos onde <TT
CLASS="literal"
>__toString</TT
> é chamado</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// __toString chamado<br /></font><font color="#007700">echo </font><font color="#0000BB">$classe</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// __toString chamado (ainda um parâmetro normal para o echo)<br /></font><font color="#007700">echo </font><font color="#DD0000">'texto'</font><font color="#007700">, </font><font color="#0000BB">$classe</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// __toString não é chamado (operador de concatenação usado primeiro)<br /></font><font color="#007700">echo </font><font color="#DD0000">'text' </font><font color="#007700">. </font><font color="#0000BB">$class</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// __toString não é chamado (converte para string primeiro)<br /></font><font color="#007700">echo (string) </font><font color="#0000BB">$class</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// __toString não é chamado (converte para string primeiro)<br /></font><font color="#007700">echo </font><font color="#DD0000">"text $class"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.magic.set-state"
><TT
CLASS="literal"
>__set_state</TT
></A
></H2
><P
>&#13;    Esse método <A
HREF="language.oop5.static.html"
>estático</A
> é chamado
    para classes exportadas por <A
HREF="function.var-export.html"
><B
CLASS="function"
>var_export()</B
></A
> desde PHP 5.1.0.
   </P
><P
>&#13;    O único parâmetro para esse método é um array contendo propriedades
    exportadas no formato <TT
CLASS="literal"
>array('property' =&#62; value, ...)</TT
>.
   </P
></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.patterns.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.final.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Patterns</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"
>Palavra-Chave 'final'</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>