Sophie

Sophie

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

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(PHP 4)</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 da Linguagem"
HREF="langref.html"><LINK
REL="PREVIOUS"
TITLE="Funções internas (built-in)"
HREF="functions.internal.html"><LINK
REL="NEXT"
TITLE="extends"
HREF="keyword.extends.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="chapter"
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="functions.internal.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="keyword.extends.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="language.oop"
>Capítulo 18. Classes e Objetos(PHP 4)</A
></H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Índice</B
></DT
><DT
><A
HREF="language.oop.html#keyword.class"
><TT
CLASS="literal"
>class</TT
></A
></DT
><DT
><A
HREF="keyword.extends.html"
><TT
CLASS="literal"
>extends</TT
></A
></DT
><DT
><A
HREF="language.oop.constructor.html"
>Construtores</A
></DT
><DT
><A
HREF="keyword.paamayim-nekudotayim.html"
><TT
CLASS="literal"
>::</TT
></A
></DT
><DT
><A
HREF="keyword.parent.html"
><TT
CLASS="literal"
>parent</TT
></A
></DT
><DT
><A
HREF="language.oop.serialization.html"
>Serializando objetos - objetos em sessões</A
></DT
><DT
><A
HREF="language.oop.magic-functions.html"
>As funções mágicas <TT
CLASS="literal"
>__sleep</TT
> e <TT
CLASS="literal"
>__wakeup</TT
></A
></DT
><DT
><A
HREF="language.oop.newref.html"
>Referências dentro do construtor</A
></DT
><DT
><A
HREF="language.oop.object-comparison.html"
>Comparando Objetos</A
></DT
></DL
></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="keyword.class"
><TT
CLASS="literal"
>class</TT
></A
></H1
><P
>&#13;    Uma classe é uma coleção de variáveis e funções trabalhando com essas
    variáveis. Uma classe é definida usando-se a seguinte sintaxe:
   </P
><P
>&#13;    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5340"
></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">CarrinhoDeCompras </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$items</font><font color="#007700">;&nbsp;&nbsp;</font><font color="#FF8000">// Itens do carrinho de compras<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;// Acrescenta a quantidade $num do artigo $artnr no carrinho<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">add_item</font><font color="#007700">(</font><font color="#0000BB">$artnr</font><font color="#007700">, </font><font color="#0000BB">$num</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">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">] += </font><font color="#0000BB">$num</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Retira a quantidade $num de artigos $artnr do carrinho<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">remove_item</font><font color="#007700">(</font><font color="#0000BB">$artnr</font><font color="#007700">, </font><font color="#0000BB">$num</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">] &gt; </font><font color="#0000BB">$num</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">] -= </font><font color="#0000BB">$num</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} elseif (</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">] == </font><font color="#0000BB">$num</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">items</font><font color="#007700">[</font><font color="#0000BB">$artnr</font><font color="#007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">false</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    Isto define uma classe chamada CarrinhoDeCompras que consiste de uma matriz associativa de
    artigos no carrinho e duas funções para acrescentar e remover itens deste
    carrinho.
   </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Atenção</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;     Você <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>NÃO</I
></SPAN
> pode quebrar uma definição de classe em
     múltiplos arquivos. Você <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>NÃO</I
></SPAN
> pode também quebrar a definição
     de uma classe em múltiplos blocos PHP, a menos que a quebra esteja dentro de uma
     declaração de método. O seguinte não irá funcionar:
    </P
><P
>&#13;     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5348"
></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">test </font><font color="#007700">{<br /></font><font color="#0000BB">?&gt;<br />&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">test</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">'OK'</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;     Entretanto, o seguinte é permitido:
    </P
><P
>&#13;     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5352"
></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">test </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">test</font><font color="#007700">() {<br /></font><font color="#0000BB">?&gt;<br />&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">print </font><font color="#DD0000">'OK'</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
></TD
></TR
></TABLE
></DIV
><P
>&#13;    Os cuidados a seguir devem ser tomados a partir do PHP 4:
   </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 nome <TT
CLASS="literal"
>stdClass</TT
> é utilizado internamente pela
     Zend e é uma palavra reservada. Você não pode ter uma classe chamada
     <TT
CLASS="literal"
>stdClass</TT
> no PHP.
    </P
></TD
></TR
></TABLE
></DIV
><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 nome de função <TT
CLASS="literal"
>__sleep</TT
> e
      <TT
CLASS="literal"
>__wakeup</TT
> são especialmente mágicos para as classes PHP. Você
      não pode ter esses nomes em nenhuma de suas
      classes a não ser que você deseje aplicar essa funcionalidade mágica
      com elas. Veja abaixo para mais detalhes.
    </P
></TD
></TR
></TABLE
></DIV
><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 reserva todos os nomes de funções começando com __ como mágicas.
      É recomendável que você não utilize nome de funções começando com
      __ no PHP a não ser que você precise dessas funcionalidades mágicas.
    </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;    No PHP 4, somente inicializações com constantes são permitidas para
    variáveis com <TT
CLASS="literal"
>var</TT
>. Para inicializar variáveis com valores
    não constantes, você precisará de uma função de inicialização chamada
    automaticamente quando o objeto for construído a partir da
    classe. Por isso, essa função é conhecida como construtor (veja baixo).
   </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5367"
></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">CarrinhoDeCompras </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Nenhuma delas funcionarão com o PHP 4 */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">var </font><font color="#0000BB">$data_de_hoje </font><font color="#007700">= </font><font color="#0000BB">date</font><font color="#007700">(</font><font color="#DD0000">"Y-m-d"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$nome </font><font color="#007700">= </font><font color="#0000BB">$primeiro_nome</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$proprietario </font><font color="#007700">= </font><font color="#DD0000">'Fred ' </font><font color="#007700">. </font><font color="#DD0000">'Jones'</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* Mas array contendo valores constantes irao */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">var </font><font color="#0000BB">$items </font><font color="#007700">= array(</font><font color="#DD0000">"VCR"</font><font color="#007700">, </font><font color="#DD0000">"TV"</font><font color="#007700">);<br />}<br /><br /></font><font color="#FF8000">/* Esta é a forma como deve ser feito */<br /></font><font color="#007700">class </font><font color="#0000BB">CarrinhoDeCompras </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$data_de_hoje</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$nome</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$proprietario</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$items </font><font color="#007700">= array(</font><font color="#DD0000">"VCR"</font><font color="#007700">, </font><font color="#DD0000">"TV"</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">CarrinhoDeCompras</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">data_de_hoje </font><font color="#007700">= </font><font color="#0000BB">date</font><font color="#007700">(</font><font color="#DD0000">"Y-m-d"</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">nome </font><font color="#007700">= </font><font color="#0000BB">$GLOBALS</font><font color="#007700">[</font><font color="#DD0000">'primeiro_nome'</font><font color="#007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/* etc. . . */<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">}<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Classes são tipos, ou seja, são apenas um modelo das variáveis
    normais. Você pode criar uma variável (ou instância) do tipo desejado com
    o operador <TT
CLASS="literal"
>new</TT
>.
   </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5371"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$carrinho </font><font color="#007700">= new </font><font color="#0000BB">CarrinhoDeCompras</font><font color="#007700">;<br /></font><font color="#0000BB">$carrinho</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 /><br /></font><font color="#0000BB">$outro_carrinho </font><font color="#007700">= new </font><font color="#0000BB">CarrinhoDeCompras</font><font color="#007700">;<br /></font><font color="#0000BB">$outro_carrinho</font><font color="#007700">-&gt;</font><font color="#0000BB">add_item</font><font color="#007700">(</font><font color="#DD0000">"0815"</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Isto cria os objetos <CODE
CLASS="varname"
>$carrinho</CODE
> e
    <CODE
CLASS="varname"
>$outro_carrinho</CODE
>, ambos a partir da classe CarrinhoDeCompras. A função
    add_item() do objeto <CODE
CLASS="varname"
>$carrinho</CODE
> foi chamada e acrescentou 1
    item do artigo número 10 a <CODE
CLASS="varname"
>$carrinho</CODE
>. 3 itens
    do artigo número 0815 foi acrescentado no <CODE
CLASS="varname"
>$outro_carrinho</CODE
>.
   </P
><P
>&#13;    Ambos, <CODE
CLASS="varname"
>$carrinho</CODE
> e <CODE
CLASS="varname"
>$outro_carrinho</CODE
>, tem
    as funções add_item(), remove_item() e a variável itens. Elas são
    funções e variáveis distintas entre si. Você pode pensar no objetos como
    os diretórios de um sistema de arquivos. Num disco você pode
    ter dois arquivos diferentes <TT
CLASS="filename"
>README.TXT</TT
>, partindo de que eles estão em diretórios
    diferentes. Da mesma forma que você teria de especificar o
    caminho completo para acessar cada arquivo a partir do diretório principal, você
    também tem de especificar o nome completo do objeto e função que você quer chamar. No PHP
    o diretório principal pode ser o escopo global de nomes, e o
    separador de diretórios <TT
CLASS="literal"
>-&#62;</TT
>. Portanto, os nomes
    <CODE
CLASS="varname"
>$carrinho-&#62;items</CODE
> e
    <CODE
CLASS="varname"
>$outro_carrinho-&#62;items</CODE
>
    são duas variáveis diferentes. Note que a variável é chamada <CODE
CLASS="varname"
>$carrinho-&#62;items</CODE
>, não
    <CODE
CLASS="varname"
>$carrinho-&#62;$items</CODE
>, mesmo porque, um nome de variável em PHP tem
    apenas um único sinal de cifrão.
   </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5388"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// correcto, apenas um $<br /></font><font color="#0000BB">$carrinho</font><font color="#007700">-&gt;</font><font color="#0000BB">items </font><font color="#007700">= array(</font><font color="#DD0000">"10" </font><font color="#007700">=&gt; </font><font color="#0000BB">1</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// inválido, porque $carrinho-&gt;$items se transforma em $carrinho-&gt;""<br /></font><font color="#0000BB">$carrinho</font><font color="#007700">-&gt;</font><font color="#0000BB">$items </font><font color="#007700">= array(</font><font color="#DD0000">"10" </font><font color="#007700">=&gt; </font><font color="#0000BB">1</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// correto, mas pode ou não ser o que você quer:<br />// $carrinho-&gt;$myvar se torna $carrinho-&gt;items<br /></font><font color="#0000BB">$myvar </font><font color="#007700">= </font><font color="#DD0000">'items'</font><font color="#007700">;<br /></font><font color="#0000BB">$carrinho</font><font color="#007700">-&gt;</font><font color="#0000BB">$myvar </font><font color="#007700">= array(</font><font color="#DD0000">"10" </font><font color="#007700">=&gt; </font><font color="#0000BB">1</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Quando definindo uma classe, você não pode saber com que nome os objetos
    serão acessados em seus programas: enquanto escrevia a classe CarrinhoDeCompras,
    é impossível saber se o objeto criado a partir dela será chamado
    <CODE
CLASS="varname"
>$carrinho</CODE
> ou <CODE
CLASS="varname"
>$outro_carrinho</CODE
> (ou ainda ambos). Assim,
    você não pode escrever <CODE
CLASS="varname"
>$carrinho&#62;items</CODE
> dentro da própria classe
    CarrinhoDeCompras. Entretanto, para poder acessar suas próprias funções e
    variáveis de dentro da classe, pode-se utilizar a pseudo-variável
    <CODE
CLASS="varname"
>$this</CODE
>, que pode ser lida como 'eu mesmo' ou 'objeto
    atual'. Dessa forma, <CODE
CLASS="varname"
>'$this-&#62;items[$artnr]</CODE
> +=
    <CODE
CLASS="varname"
>$num'</CODE
> pode  ser lido como 'acrescente <CODE
CLASS="varname"
>$num</CODE
> para
    o contador <CODE
CLASS="varname"
>$artnr</CODE
> do meu array items' ou 'acrescente
    <CODE
CLASS="varname"
>$num</CODE
> para o contador <CODE
CLASS="varname"
>$artnr</CODE
> do array
    items do objeto atual'.
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
     A pseudo-variável <CODE
CLASS="varname"
>$this</CODE
> não é normalmente
     definida em um método que seja chamado estaticamente. Isto não é,
     entretanto, uma regra estrita: <CODE
CLASS="varname"
>$this</CODE
> é definido se um método
     é chamado estaticamente a partir de outro objeto. Neste caso, o valor de
     <CODE
CLASS="varname"
>$this</CODE
> será do objeto que o chamou. Isto é
     ilustrado no exemplo a seguir:
      <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN5406"
></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">foo</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (isset(</font><font color="#0000BB">$this</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">'$this is defined ('</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$this</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">")\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"\$this is not defined.\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">B<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">bar</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">A</font><font color="#007700">::</font><font color="#0000BB">foo</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$a </font><font color="#007700">= new </font><font color="#0000BB">A</font><font color="#007700">();<br /></font><font color="#0000BB">$a</font><font color="#007700">-&gt;</font><font color="#0000BB">foo</font><font color="#007700">();<br /></font><font color="#0000BB">A</font><font color="#007700">::</font><font color="#0000BB">foo</font><font color="#007700">();<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">$b</font><font color="#007700">-&gt;</font><font color="#0000BB">bar</font><font color="#007700">();<br /></font><font color="#0000BB">B</font><font color="#007700">::</font><font color="#0000BB">bar</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"
>$this is defined (a)
$this is not defined.
$this is defined (b)
$this is not defined.</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
    Há funções muito boas para manipulação de classes e objetos. Dê uma
    olhada em <A
HREF="ref.classobj.html"
>Funções de
    Classes e Objetos</A
>
    </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="functions.internal.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.extends.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Funções internas (built-in)</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="langref.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><TT
CLASS="literal"
>extends</TT
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>