<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >变量函数</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="函数" HREF="language.functions.html"><LINK REL="PREVIOUS" TITLE="返回值" HREF="functions.returning-values.html"><LINK REL="NEXT" TITLE="内部(内置)函数" 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" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="functions.returning-values.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 17. 函数</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="functions.internal.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="functions.variable-functions" >变量函数</A ></H1 ><P > PHP 支持变量函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且将尝试执行它。除了别的事情以外,这个可以被用于实现回调函数,函数表等等。 </P ><P > 变量函数不能用于语言结构,例如 <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 > 以及类似的语句。需要使用自己的外壳函数来将这些结构用作变量函数。 </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5414" ></A ><P ><B >例 17-14. 变量函数示例</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">"In 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 /> echo </font><font color="#DD0000">"In bar(); argument was '$arg'.<br /></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// This is a wrapper function around 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">// This calls 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">// This calls 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">// This calls echoit()<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 还可以利用变量函数的特性来调用一个对象的方法。 <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5418" ></A ><P ><B >例 17-15. 变量方法范例</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">Variable</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">// This calls the Bar() method<br /> </font><font color="#007700">}<br /><br /> function </font><font color="#0000BB">Bar</font><font color="#007700">()<br /> {<br /> echo </font><font color="#DD0000">"This is Bar"</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">"Variable"</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">// This calls $foo->Variable()<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 请参阅 <A HREF="function.call-user-func.html" ><B CLASS="function" >call_user_func()</B ></A >,<A HREF="language.variables.variable.html" >可变变量</A >和 <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" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="functions.internal.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >返回值</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.functions.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >内部(内置)函数</TD ></TR ></TABLE ></DIV ></BODY ></HTML >