Sophie

Sophie

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

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
>Indução de Tipo</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="Reflexão"
HREF="language.oop5.reflection.html"><LINK
REL="NEXT"
TITLE="Exceções"
HREF="language.exceptions.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.reflection.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.exceptions.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.typehinting"
>Indução de Tipo</A
></H1
><P
>&#13;   PHP 5 introduz Indução de Tipo. Funções podem forçar que os parâmetros
   sejam objetos (especificando o nome da classe no protótipo da
   função) ou array (a partir do PHP 5.1).
  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN6143"
></A
><P
><B
>Exemplo 19-41. Exemplo Indução de Tipo</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">// Uma classe de exemplo<br /></font><font color="#007700">class </font><font color="#0000BB">MinhaClasse<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* Uma função de teste<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* Primeiro parâmetro deve ser um objeto do tipo OutraClasse<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">teste</font><font color="#007700">(</font><font color="#0000BB">OutraClasse $outraclasse</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$outraclasse</font><font color="#007700">&gt;var;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;* Outra função de teste<br />&nbsp;&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;&nbsp;* Primeiro parâmetro deve ser um array<br />&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">testa_array</font><font color="#007700">(array </font><font color="#0000BB">$array_de_entrada</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$array_de_entrada</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">// Outro classe de exemplo<br /></font><font color="#007700">class </font><font color="#0000BB">OutraClasse </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $var </font><font color="#007700">= </font><font color="#DD0000">'Alô Mundo'</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;    Não satisfazer a indução de tipo resulta em erro fatal.
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Uma instância de cada classe<br /></font><font color="#0000BB">$minhaclasse </font><font color="#007700">= new </font><font color="#0000BB">MinhaClasse</font><font color="#007700">;<br /></font><font color="#0000BB">$outraclasse </font><font color="#007700">= new </font><font color="#0000BB">OutraClasse</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Erro Fatal: Argumento 1 deve ser um objeto da classe OutraClasse<br /></font><font color="#0000BB">$minhaclasse</font><font color="#007700">-&gt;</font><font color="#0000BB">teste</font><font color="#007700">(</font><font color="#DD0000">'hello'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Erro Fatal: Argumento 1 deve ser uma instância de OutraClasse<br /></font><font color="#0000BB">$foo </font><font color="#007700">= new </font><font color="#0000BB">stdClass</font><font color="#007700">;<br /></font><font color="#0000BB">$minhaclasse</font><font color="#007700">-&gt;</font><font color="#0000BB">teste</font><font color="#007700">(</font><font color="#0000BB">$foo</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Erro Fatal: Argumento 1 deve ser diferente de null<br /></font><font color="#0000BB">$minhaclasse</font><font color="#007700">-&gt;</font><font color="#0000BB">teste</font><font color="#007700">(</font><font color="#0000BB">null</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Funciona: Imprime Alô Mundo<br /></font><font color="#0000BB">$minhaclasse</font><font color="#007700">-&gt;</font><font color="#0000BB">teste</font><font color="#007700">(</font><font color="#0000BB">$outraclasse</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Erro Fatal: Argumento 1 deve ser um array<br /></font><font color="#0000BB">$minhaclasse</font><font color="#007700">-&gt;</font><font color="#0000BB">testa_array</font><font color="#007700">(</font><font color="#DD0000">'a string'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Funciona: Imprime o array<br /></font><font color="#0000BB">$minhaclasse</font><font color="#007700">-&gt;</font><font color="#0000BB">testa_array</font><font color="#007700">(array(</font><font color="#DD0000">'a'</font><font color="#007700">, </font><font color="#DD0000">'b'</font><font color="#007700">, </font><font color="#DD0000">'c'</font><font color="#007700">));<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;    Indução de tipo também funciona com funções:
   </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Um exemplo de classe<br /></font><font color="#007700">class </font><font color="#0000BB">MinhaClasse </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public $var </font><font color="#007700">= </font><font color="#DD0000">'Alô Mundo'</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">/**<br /> * Uma função de teste<br /> *<br /> * Primeiro parâmetro deve ser um objeto do tipo MinhaClasse<br /> */<br /></font><font color="#007700">function </font><font color="#0000BB">MinhaFuncao </font><font color="#007700">(</font><font color="#0000BB">MinhaClasse $foo</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$foo</font><font color="#007700">-&gt;</font><font color="#0000BB">var</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// Funciona<br /></font><font color="#0000BB">$minhaclasse </font><font color="#007700">= new </font><font color="#0000BB">MinhaClasse</font><font color="#007700">;<br /></font><font color="#0000BB">MinhaFuncao</font><font color="#007700">(</font><font color="#0000BB">$minhaclasse</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;   Indução de tipo só pode ser usado com tipos objetos. Indução
   tradicional com <A
HREF="language.types.integer.html"
><B
CLASS="type"
>int</B
></A
> e <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
> não são
   suportados.
  </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="language.oop5.reflection.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.exceptions.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Reflexão</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"
>Exceções</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>