Sophie

Sophie

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

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</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="extends"
HREF="keyword.extends.html"><LINK
REL="NEXT"
TITLE="::"
HREF="keyword.paamayim-nekudotayim.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="keyword.extends.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="keyword.paamayim-nekudotayim.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.constructor"
>Construtores</A
></H1
><P
>&#13;    Construtores são funções numa classe que são automaticamente
    chamados quando você cria uma nova instância da classe com
    <TT
CLASS="literal"
>new</TT
>. Uma função se torna um construtor quando ele tem o mesmo nome
    da classe Se uma classe.
    não tiver um construtor, o construtor da classe base será
    chamado, se existir.
   </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5437"
></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">CarrinhoDeComprasAutomatico </font><font color="#007700">extends </font><font color="#0000BB">CarrinhoDeCompras </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">CarrinhoDeComprasAutomatico</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">add_item </font><font color="#007700">(</font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">1</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
>&#13;    Isto define uma classe chamada CarrinhoDeComprasAutomatico que é um CarrinhoDeCompras mais um construtor
    que inicializa o carrinho com um item do artigo numero "10"
    cada vez que um novo CarrinhoDeComprasAutomatico for criado com "new". Construtores
    pode ter argumentos e esses argumentos podem ser opcionais, o que os
    torna muito mais práticos. Para permitir a criação de classes
    sem parâmetros, todos os parâmetros dos construtores pode ser
    feitos opcionais simplesmente por terem valores default.
   </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5440"
></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">CarrinhoDeComprasComConstrutor </font><font color="#007700">extends </font><font color="#0000BB">CarrinhoDeCompras </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">CarrinhoDeComprasComConstrutor</font><font color="#007700">(</font><font color="#0000BB">$item </font><font color="#007700">= </font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">$num </font><font color="#007700">= </font><font color="#0000BB">1</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">add_item </font><font color="#007700">(</font><font color="#0000BB">$item</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">// Fazendo compras do mesmo modo antigo<br /></font><font color="#0000BB">$carrinho_default </font><font color="#007700">= new </font><font color="#0000BB">CarrinhoDeComprasComConstrutor</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Fazendo compres de verdade<br /></font><font color="#0000BB">$carrinhho_diferente </font><font color="#007700">= new </font><font color="#0000BB">CarrinhoDeComprasComConstrutor</font><font color="#007700">(</font><font color="#DD0000">"20"</font><font color="#007700">, </font><font color="#0000BB">17</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Você pode usar o operador <TT
CLASS="literal"
>@</TT
> para
    <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>esconder</I
></SPAN
> erros gerados no construtor do objeto,
    por exemplo: <TT
CLASS="literal"
>@new</TT
>.
   </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5446"
></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<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">A</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Eu sou o construtor de A.&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">B</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Eu sou uma função normal chamada B na classe A.&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Eu não sou o construtor de A.&lt;br /&gt;\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">B </font><font color="#007700">extends </font><font color="#0000BB">A<br /></font><font color="#007700">{<br />}<br /><br /></font><font color="#FF8000">// Isto irá chamar B() como um construtor.<br /></font><font color="#0000BB">$b </font><font color="#007700">= new </font><font color="#0000BB">B</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    A função B() na classe A se tornará silenciosamente um
    construtor na classe B, mesmo que isso nunca tenha sido planejado.
    O PHP 4 não se importa se a função
    foi definida na classe B, ou se ela foi herdada.
   </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;     O PHP 4 não chama automaticamente os construtores da
     classe base a partir do construtor da classe derivada. É de sua
     responsabilidade propagar a chamada dos construtores
     herança acima, onde apropriado.
    </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;    Destrutores são funções que são chamadas automaticamente
    quando um objeto é destruído, ou pela utilização de <A
HREF="function.unset.html"
><B
CLASS="function"
>unset()</B
></A
>
    ou pela simples saída do escopo. Não existem destrutores
    no PHP. Você pode usar <A
HREF="function.register-shutdown-function.html"
><B
CLASS="function"
>register_shutdown_function()</B
></A
>
    ao invés para simular a maioria dos efeitos dos destruidores.
   </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="keyword.extends.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="keyword.paamayim-nekudotayim.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><TT
CLASS="literal"
>extends</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"
><TT
CLASS="literal"
>::</TT
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>