Sophie

Sophie

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

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
>Classes e 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="Referência das Funções"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="ccvs_void"
HREF="function.ccvs-void.html"><LINK
REL="NEXT"
TITLE="call_user_method_array"
HREF="function.call-user-method-array.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="reference"
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="function.ccvs-void.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.call-user-method-array.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.classobj"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>XII. Classes e Objetos</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN15442"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.intro"
>Introdução</A
></H1
><P
>&#13;      Estas funções te permitem obter informações sobre classes
      e instâncias de objetos. Você pode obter o nome da classe
      ao qual um objeto pertence, bem como suas propriedades e
      métodos. Usando estas funções, você pode descobrir não apenas
      a classe de um objeto, mas também seu parentesco
     (no caso, que classe este objeto de classe se estende).
     </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.requirements"
>Dependências</A
></H1
><P
>Nenhuma biblioteca externa é necessária para compilar esta extensão.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.installation"
>Instalação</A
></H1
><P
>Não há nenhuma instalação necessária para utilizar estas
 funções, elas fazem parte do núcleo do PHP.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.configuration"
>Configurações em execução</A
></H1
><P
>Esta extensão não define nenhum parâmetro de configuração no <TT
CLASS="filename"
>php.ini</TT
>.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.resources"
>Tipos Resource</A
></H1
><P
>Esta extensão não possui nenhum tipo resource.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.constants"
>Constantes pré-definidas</A
></H1
><P
>Esta extensão não possui nenhuma constante.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.examples"
>Exemplos</A
></H1
><P
>&#13;      Neste exemplo, nós primeiramente definiremos uma classe base e uma
      extensão da classe. A classe base descreve um vegetal comum,
      que é comestível ou não e que tem sua cor. A sub-classe
      <CODE
CLASS="varname"
>Espinafre</CODE
> adiciona um método para cozinhá-lo e outro
      para descobrir se ele está cozido.
     </P
><P
>&#13;      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN15467"
></A
><P
><B
>Exemplo 1. classes.inc</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br /></font><font color="#FF8000">// Classe base com propriedades e métodos de membros<br /></font><font color="#007700">class </font><font color="#0000BB">Vegetal </font><font color="#007700">{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$comestivel</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$cor</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">Vegetal</font><font color="#007700">( </font><font color="#0000BB">$comestivel</font><font color="#007700">, </font><font color="#0000BB">$cor</font><font color="#007700">=</font><font color="#DD0000">"verde" </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">comestivel </font><font color="#007700">= </font><font color="#0000BB">$comestivel</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">cor </font><font color="#007700">= </font><font color="#0000BB">$cor</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">is_comestivel</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">comestivel</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">qual_cor</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">cor</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />} </font><font color="#FF8000">// Fim da classe Vegetal<br /><br />// estender a classe base<br /></font><font color="#007700">class </font><font color="#0000BB">Espinafre estende Vegetal </font><font color="#007700">{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$cozido </font><font color="#007700">= </font><font color="#0000BB">false</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">Espinafre</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">Vegetal</font><font color="#007700">( </font><font color="#0000BB">true</font><font color="#007700">, </font><font color="#DD0000">"verde" </font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">cozinhe</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">cozido </font><font color="#007700">= </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">esta_cozido</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">cozido</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />} </font><font color="#FF8000">// Fim da classe Espinafre<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
><P
>&#13;     Nós instânciamos 2 objetos destas classes e exibimos informações
     sobre elas, incluindo o parentesco de suas classes.
     Nós também definimos algumas funções úteis, principalmente para ter
     uma boa saída de variáveis.
     </P
><P
>&#13;      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN15472"
></A
><P
><B
>Exemplo 2. test_script.php</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
&lt;pre&gt;<br /><font color="#0000BB">&lt;?php<br /><br /></font><font color="#007700">include </font><font color="#DD0000">"classes.inc"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Funções utilitárias<br /><br /></font><font color="#007700">function </font><font color="#0000BB">print_vars</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$arr </font><font color="#007700">= </font><font color="#0000BB">get_object_vars</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;while (list(</font><font color="#0000BB">$prop</font><font color="#007700">, </font><font color="#0000BB">$val</font><font color="#007700">) = </font><font color="#0000BB">each</font><font color="#007700">(</font><font color="#0000BB">$arr</font><font color="#007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"</font><font color="#007700">\t</font><font color="#DD0000">$prop = $val</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">print_methods</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$arr </font><font color="#007700">= </font><font color="#0000BB">get_class_methods</font><font color="#007700">(</font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">$arr </font><font color="#007700">as </font><font color="#0000BB">$method</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"</font><font color="#007700">\t</font><font color="#DD0000">function $method()</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">class_parentesco</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">, </font><font color="#0000BB">$class</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">is_subclass_of</font><font color="#007700">($</font><font color="#0000BB">$obj</font><font color="#007700">, </font><font color="#0000BB">$class</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Objeto $obj pertence à classe "</font><font color="#007700">.</font><font color="#0000BB">get_class</font><font color="#007700">($</font><font color="#0000BB">$obj</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">" uma subclasse de $class</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Objeto $obj não pertence a uma subclasse de $class</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">// instanciar 2 objetos<br /><br /></font><font color="#0000BB">$vegetariano </font><font color="#007700">= new </font><font color="#0000BB">Vegetal</font><font color="#007700">(</font><font color="#0000BB">true</font><font color="#007700">, </font><font color="#DD0000">"azul"</font><font color="#007700">);<br /></font><font color="#0000BB">$folheado </font><font color="#007700">= new </font><font color="#0000BB">Espinafre</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// Exibe informação sobre os objetos<br /></font><font color="#007700">echo </font><font color="#DD0000">"vegetariano: CLASS " </font><font color="#007700">. </font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$vegetariano</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />echo </font><font color="#DD0000">"folheado: CLASS " </font><font color="#007700">. </font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$folheado</font><font color="#007700">);<br />echo </font><font color="#DD0000">", PARENTE " </font><font color="#007700">. </font><font color="#0000BB">get_parent_class</font><font color="#007700">(</font><font color="#0000BB">$folheado</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Mostra as propriedades de vegetariano<br /></font><font color="#007700">echo </font><font color="#DD0000">"\nvegetariano: Propriedades\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_vars</font><font color="#007700">(</font><font color="#0000BB">$vegetariano</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// e os métodos de folheado<br /></font><font color="#007700">echo </font><font color="#DD0000">"\nfolheado: Métodos\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_methods</font><font color="#007700">(</font><font color="#0000BB">$folheado</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"\nParentesco:\n"</font><font color="#007700">;<br /></font><font color="#0000BB">class_parentesco</font><font color="#007700">(</font><font color="#DD0000">"folheado"</font><font color="#007700">, </font><font color="#DD0000">"Espinafre"</font><font color="#007700">);<br /></font><font color="#0000BB">class_parentesco</font><font color="#007700">(</font><font color="#DD0000">"folheado"</font><font color="#007700">, </font><font color="#DD0000">"Vegetal"</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;<br /></font>&lt;/pre&gt;</font>
</code></TD
></TR
></TABLE
><P
>&#13;        Uma coisa importante para se notar no exemplo acima é que
        o objeto <CODE
CLASS="varname"
>$folheado</CODE
> é uma instância da classe
        <B
CLASS="classname"
>Espinafre</B
> que é uma subclasse de
        <B
CLASS="classname"
>Vegetal</B
>,
        então a última parte do script acima exibirá:
       </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>[...]
Parentesco:
Objeto folheado não pertence a uma subclasse de Espinafre
Object folheado pertence à classe Espinafre uma subclasse de Vegetal</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Índice</B
></DT
><DT
><A
HREF="function.call-user-method-array.html"
>call_user_method_array</A
>&nbsp;--&nbsp;
     Chama métodos de usuário dado uma matriz de parâmetros [obsoleto]
    </DT
><DT
><A
HREF="function.call-user-method.html"
>call_user_method</A
>&nbsp;--&nbsp;
     Chama um método de usuário num objeto específico [obsoleto]
    </DT
><DT
><A
HREF="function.class-exists.html"
>class_exists</A
>&nbsp;--&nbsp;Checa se uma classe foi definida</DT
><DT
><A
HREF="function.get-class-methods.html"
>get_class_methods</A
>&nbsp;--&nbsp;Retorna uma matriz associativa com nomes de métodos da classe</DT
><DT
><A
HREF="function.get-class-vars.html"
>get_class_vars</A
>&nbsp;--&nbsp;
     Retorna uma matriz das propriedades padrão da classe
    </DT
><DT
><A
HREF="function.get-class.html"
>get_class</A
>&nbsp;--&nbsp;Retorna o nome da classe de um objeto</DT
><DT
><A
HREF="function.get-declared-classes.html"
>get_declared_classes</A
>&nbsp;--&nbsp;Retorna uma matriz com os nomes das classes definidas</DT
><DT
><A
HREF="function.get-declared-interfaces.html"
>get_declared_interfaces</A
>&nbsp;--&nbsp;
     Retorna um array com todas as interfaces declaradas
    </DT
><DT
><A
HREF="function.get-object-vars.html"
>get_object_vars</A
>&nbsp;--&nbsp;Retorna uma matriz associativa com as propriedades do objeto</DT
><DT
><A
HREF="function.get-parent-class.html"
>get_parent_class</A
>&nbsp;--&nbsp;Devolve o nome da classe pai para o objeto ou classe</DT
><DT
><A
HREF="function.interface-exists.html"
>interface_exists</A
>&nbsp;--&nbsp;Checa se a interface foi definida</DT
><DT
><A
HREF="function.is-a.html"
>is_a</A
>&nbsp;--&nbsp;
     Retorna <TT
CLASS="constant"
><B
>TRUE</B
></TT
> se o objeto é desta classe ou tem esta classe
	 como sua classe pai.
    </DT
><DT
><A
HREF="function.is-subclass-of.html"
>is_subclass_of</A
>&nbsp;--&nbsp;
     Retorna <TT
CLASS="constant"
><B
>TRUE</B
></TT
> se o objeto tem esta classe como uma de suas classes pai 
    </DT
><DT
><A
HREF="function.method-exists.html"
>method_exists</A
>&nbsp;--&nbsp;Checa se o método da classe existe</DT
><DT
><A
HREF="function.property-exists.html"
>property_exists</A
>&nbsp;--&nbsp;
   Checa se o objeto ou a classe tem uma propriedade
  </DT
></DL
></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="function.ccvs-void.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="function.call-user-method-array.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>ccvs_void</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>call_user_method_array</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>