<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Funções variáveis</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="Funções" HREF="language.functions.html"><LINK REL="PREVIOUS" TITLE="Retornando valores" HREF="functions.returning-values.html"><LINK REL="NEXT" TITLE="Funções internas (built-in)" HREF="functions.internal.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="functions.returning-values.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Capítulo 17. Funções</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="functions.internal.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="functions.variable-functions" >Funções variáveis</A ></H1 ><P > O PHP suporta o conceito de funções variáveis. Isto significa que se um nome de variável tem parênteses no final dela, o PHP procurará uma função com o mesmo nome, qualquer que seja a avaliação da variável, e tentará executá-la. Entre outras coisas, isto pode ser usado para implementar callbacks, tabelas de função e assim por diante. </P ><P > Funções variáveis não funcionam com construtores de linguagem como <A HREF="function.echo.html" ><B CLASS="function" >echo()</B ></A >, <A HREF="function.print.html" ><B CLASS="function" >print()</B ></A >, <A HREF="function.unset.html" ><B CLASS="function" >unset()</B ></A >, <A HREF="function.isset.html" ><B CLASS="function" >isset()</B ></A >, <A HREF="function.empty.html" ><B CLASS="function" >empty()</B ></A >, <A HREF="function.include.html" ><B CLASS="function" >include()</B ></A >, <A HREF="function.require.html" ><B CLASS="function" >require()</B ></A > e outras assim. Você precisa antes construir uma função interceptadora (wrapper) para utilizar qualquer um desses construtores como funções convencionais. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5294" ></A ><P ><B >Exemplo 17-14. Exemplo de funções variáveis</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">foo</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"Chamou foo()<br>\n"</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">bar</font><font color="#007700">(</font><font color="#0000BB">$arg </font><font color="#007700">= </font><font color="#DD0000">''</font><font color="#007700">)<br />{<br /> echo </font><font color="#DD0000">"Chamou bar(); com argumento '$arg'.<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// Essa eh uma funcao wrapper para echo()<br /></font><font color="#007700">function </font><font color="#0000BB">echoit</font><font color="#007700">(</font><font color="#0000BB">$string</font><font color="#007700">)<br />{<br /> echo </font><font color="#0000BB">$string</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$func </font><font color="#007700">= </font><font color="#DD0000">'foo'</font><font color="#007700">;<br /></font><font color="#0000BB">$func</font><font color="#007700">(); </font><font color="#FF8000">// Chama foo()<br /><br /></font><font color="#0000BB">$func </font><font color="#007700">= </font><font color="#DD0000">'bar'</font><font color="#007700">;<br /></font><font color="#0000BB">$func</font><font color="#007700">(</font><font color="#DD0000">'test'</font><font color="#007700">); </font><font color="#FF8000">// Chama bar()<br /><br /></font><font color="#0000BB">$func </font><font color="#007700">= </font><font color="#DD0000">'echoit'</font><font color="#007700">;<br /></font><font color="#0000BB">$func</font><font color="#007700">(</font><font color="#DD0000">'test'</font><font color="#007700">); </font><font color="#FF8000">// Chama echoit()<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Você também pode chamar métodos de objetos utilizando o recurso de funções variáveis. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5298" ></A ><P ><B >Exemplo 17-15. Exemplo de chama de método variável</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">Foo<br /></font><font color="#007700">{<br /> function </font><font color="#0000BB">MetodoVariavel</font><font color="#007700">()<br /> {<br /> </font><font color="#0000BB">$name </font><font color="#007700">= </font><font color="#DD0000">'Bar'</font><font color="#007700">;<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">$name</font><font color="#007700">(); </font><font color="#FF8000">// Isto chama o método Bar()<br /> </font><font color="#007700">}<br /><br /> function </font><font color="#0000BB">Bar</font><font color="#007700">()<br /> {<br /> echo </font><font color="#DD0000">"Bar foi chamada!"</font><font color="#007700">;<br /> }<br />}<br /><br /></font><font color="#0000BB">$foo </font><font color="#007700">= new </font><font color="#0000BB">Foo</font><font color="#007700">();<br /></font><font color="#0000BB">$funcname </font><font color="#007700">= </font><font color="#DD0000">"MetodoVariavel"</font><font color="#007700">;<br /></font><font color="#0000BB">$foo</font><font color="#007700">-></font><font color="#0000BB">$funcname</font><font color="#007700">(); </font><font color="#FF8000">// Isto chama $foo->MetodoVariavel()<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Veja também <A HREF="function.call-user-func.html" ><B CLASS="function" >call_user_func()</B ></A >, <A HREF="language.variables.variable.html" > variáveis variáveis</A > e <A HREF="function.function-exists.html" ><B CLASS="function" >function_exists()</B ></A >. </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="functions.returning-values.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="functions.internal.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Retornando valores</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.functions.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Funções internas (built-in)</TD ></TR ></TABLE ></DIV ></BODY ></HTML >