Sophie

Sophie

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

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
>Construtores and Destrutores</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="Autoloading Objects"
HREF="language.oop5.autoload.html"><LINK
REL="NEXT"
TITLE="Visibilidade"
HREF="language.oop5.visibility.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.autoload.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.visibility.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.decon"
>Construtores and Destrutores</A
></H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.decon.constructor"
>Construtores</A
></H2
>void <B
CLASS="methodname"
>__construct</B
> ( [mixed args [, ...]] )<BR
></BR
><P
>&#13;    PHP 5 permite que os desenvolvedores declarem métodos construtores para as classes.
    Classes que tem um método construtor chamam esse método cada vez
    que um objeto novo é criado, então é apropriado para qualquer inicialização que o
    objeto possa vir a precisar antes de ser usado.
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
     Construtores pais não são chamados implicitamente se a classe filha define
     um construtor. Para executar o construtor da classe pai, uma chamada a
     <B
CLASS="function"
>parent::__construct()</B
> dentro do construtor da classe filha
     é necessária.
    </P
></BLOCKQUOTE
></DIV
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5638"
></A
><P
><B
>Exemplo 19-7. Usando novos construtores unificados</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">ClasseBase </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__construct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"No construtor da ClasseBase\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">SubClasse </font><font color="#007700">extends </font><font color="#0000BB">ClasseBase </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__construct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">parent</font><font color="#007700">::</font><font color="#0000BB">__construct</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"No construtor da SubClasse\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">ClasseBase</font><font color="#007700">();<br /></font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">SubClasse</font><font color="#007700">();<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    Para garantir compatibilidade reversa, se o PHP 5 não conseguir achar uma
    função <B
CLASS="function"
>__construct()</B
> para uma determinada classe, ele procurará
    pela função construtora à moda-antiga, que tenha o mesmo nome da classe.
    Efetivamente, significa que o único caso que pode gerar problemas de
    compatibilidade seria se a classe tiver um método chamado
    <B
CLASS="function"
>__construct()</B
> que fosse usado para outra finalidade que não inicializar o objeto.
   </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.decon.destructor"
>Destrutor</A
></H2
>void <B
CLASS="methodname"
>__destruct</B
> ( void  )<BR
></BR
><P
>&#13;    PHP 5 introduz um conceito de destrutor similar ao de outras
    linguagens orientadas a objeto, como o Java. O método destrutor será chamado
    assim que todas as referências a um objeto particular forem removidas ou quando
    o objeto for explicitamente destruído.
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5651"
></A
><P
><B
>Exemplo 19-8. Exemplo de Destrutor</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">MinhaClasseDestruivel </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__construct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"No construtor\n"</font><font color="#007700">;<br />&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="#DD0000">"MinhaClasseDestruivel"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__destruct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"Destruindo " </font><font color="#007700">. </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">name </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">MinhaClasseDestruivel</font><font color="#007700">();<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    Como os construtores, destrutores pais não serão chamados implicitamente pelo
    engine. Para executar o destrutor pai, deve-se fazer uma chamada
    explicitamente a <B
CLASS="function"
>parent::__destruct()</B
> no corpo do
    destrutor.
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
     O método destrutor é chamado durante a saída do script para que cabeçalhos
     sempre sejam enviados.
    </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
     Tentar disparar uma exceção em um destrutor causa um erro fatal.
    </P
></BLOCKQUOTE
></DIV
></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.autoload.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.visibility.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Autoloading Objects</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"
>Visibilidade</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>