<HTML ><HEAD ><TITLE >Variable functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Functions" HREF="functions.html"><LINK REL="PREVIOUS" TITLE="old_function" HREF="functions.old-syntax.html"><LINK REL="NEXT" TITLE="Classes and Objects" HREF="language.oop.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="functions.old-syntax.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 13. Functions</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.oop.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="functions.variable-functions" ></A >Variable functions</H1 ><P > PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth. </P ><P > Variable functions won't work with language constructs such as <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 > and the like. You need to use your own wrapper function to utilize any of these constructs as variable functions. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5416" ></A ><P ><B >Example 13-3. Variable function example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php function foo() { echo "In foo()<br>\n"; } function bar($arg = '') { echo "In bar(); argument was '$arg'.<br>\n"; } // This is a wrapper function around echo function echoit($string) { echo $string; } $func = 'foo'; $func(); // This calls foo() $func = 'bar'; $func('test'); // This calls bar() $func = 'echoit'; $func('test'); // This calls echoit() ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > You can also call an object's method by using the variable functions feature. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5420" ></A ><P ><B >Example 13-4. Variable method example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php class Foo { function Var() { $name = 'Bar'; $this->$name(); // This calls the Bar() method } function Bar() { echo "This is Bar"; } } $foo = new Foo(); $funcname = "Var"; $foo->$varname(); // This calls $foo->Var() ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.call-user-func.html" ><B CLASS="function" >call_user_func()</B ></A >, <A HREF="language.variables.variable.html" > variable variables</A > and <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.old-syntax.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.oop.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="literal" >old_function</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="functions.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Classes and Objects</TD ></TR ></TABLE ></DIV ></BODY ></HTML >