<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Call the callback given by the first parameter</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.call-user-func-array.html">call_user_func_array</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.create-function.html">create_function</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.call-user-func" class="refentry"> <div class="refnamediv"> <h1 class="refname">call_user_func</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">call_user_func</span> — <span class="dc-title">Call the callback given by the first parameter</span></p> </div> <div class="refsect1 description" id="refsect1-function.call-user-func-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>call_user_func</strong></span> ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$parameter</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span> ]] )</div> <p class="para rdfs-comment"> Calls the <em><code class="parameter">callback</code></em> given by the first parameter and passes the remaining parameters as arguments. </p> </div> <div class="refsect1 parameters" id="refsect1-function.call-user-func-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">callback</code></em></span> <dd> <p class="para"> The <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> to be called. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">parameter</code></em></span> <dd> <p class="para"> Zero or more parameters to be passed to the callback. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Note that the parameters for <span class="function"><strong>call_user_func()</strong></span> are not passed by reference. <div class="example" id="example-5098"> <p><strong>Example #1 <span class="function"><strong>call_user_func()</strong></span> example and references</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br />function </span><span style="color: #0000BB">increment</span><span style="color: #007700">(&</span><span style="color: #0000BB">$var</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #0000BB">$var</span><span style="color: #007700">++;<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'increment'</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'increment'</span><span style="color: #007700">, array(&</span><span style="color: #0000BB">$a</span><span style="color: #007700">)); </span><span style="color: #FF8000">// You can use this instead before PHP 5.3<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">.</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 0 1 </pre></div> </div> </div> </p> </p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.call-user-func-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the return value of the callback, or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 changelog" id="refsect1-function.call-user-func-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> The interpretation of object oriented keywords like <em>parent</em> and <em>self</em> has changed. Previously, calling them using the double colon syntax would emit an <strong><code>E_STRICT</code></strong> warning because they were interpreted as static. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.call-user-func-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5099"> <p><strong>Example #2 <span class="function"><strong>call_user_func()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">barber</span><span style="color: #007700">(</span><span style="color: #0000BB">$type</span><span style="color: #007700">)<br />{<br /> echo </span><span style="color: #DD0000">"You wanted a </span><span style="color: #0000BB">$type</span><span style="color: #DD0000"> haircut, no problem\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'barber'</span><span style="color: #007700">, </span><span style="color: #DD0000">"mushroom"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'barber'</span><span style="color: #007700">, </span><span style="color: #DD0000">"shave"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> You wanted a mushroom haircut, no problem You wanted a shave haircut, no problem </pre></div> </div> </div> <div class="example" id="example-5100"> <p><strong>Example #3 <span class="function"><strong>call_user_func()</strong></span> using namespace name</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">Foobar</span><span style="color: #007700">;<br /><br />class </span><span style="color: #0000BB">Foo </span><span style="color: #007700">{<br /> static public function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> print </span><span style="color: #DD0000">"Hello world!\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #0000BB">__NAMESPACE__ </span><span style="color: #007700">.</span><span style="color: #DD0000">'\Foo::test'</span><span style="color: #007700">); </span><span style="color: #FF8000">// As of PHP 5.3.0<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">__NAMESPACE__ </span><span style="color: #007700">.</span><span style="color: #DD0000">'\Foo'</span><span style="color: #007700">, </span><span style="color: #DD0000">'test'</span><span style="color: #007700">)); </span><span style="color: #FF8000">// As of PHP 5.3.0<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Hello world! Hello world! </pre></div> </div> </div> <div class="example" id="example-5101"> <p><strong>Example #4 Using a class method with <span class="function"><strong>call_user_func()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">myclass </span><span style="color: #007700">{<br /> static function </span><span style="color: #0000BB">say_hello</span><span style="color: #007700">()<br /> {<br /> echo </span><span style="color: #DD0000">"Hello!\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$classname </span><span style="color: #007700">= </span><span style="color: #DD0000">"myclass"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$classname</span><span style="color: #007700">, </span><span style="color: #DD0000">'say_hello'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #0000BB">$classname </span><span style="color: #007700">.</span><span style="color: #DD0000">'::say_hello'</span><span style="color: #007700">); </span><span style="color: #FF8000">// As of 5.2.3<br /><br /></span><span style="color: #0000BB">$myobject </span><span style="color: #007700">= new </span><span style="color: #0000BB">myclass</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$myobject</span><span style="color: #007700">, </span><span style="color: #DD0000">'say_hello'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Hello! Hello! Hello! </pre></div> </div> </div> <div class="example" id="example-5102"> <p><strong>Example #5 Using lambda function with <span class="function"><strong>call_user_func()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />call_user_func</span><span style="color: #007700">(function(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">) { print </span><span style="color: #DD0000">"[</span><span style="color: #0000BB">$arg</span><span style="color: #DD0000">]\n"</span><span style="color: #007700">; }, </span><span style="color: #DD0000">'test'</span><span style="color: #007700">); </span><span style="color: #FF8000">/* As of PHP 5.3.0 */<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> [test] </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.call-user-func-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">Callbacks registered with functions such as <span class="function"><strong>call_user_func()</strong></span> and <span class="function"><a href="function.call-user-func-array.html" class="function">call_user_func_array()</a></span> will not be called if there is an uncaught exception thrown in a previous callback.</p></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.call-user-func-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.call-user-func-array.html" class="function" rel="rdfs-seeAlso">call_user_func_array()</a> - Call a callback with an array of parameters</span></li> <li class="member"> <span class="function"><a href="function.is-callable.html" class="function" rel="rdfs-seeAlso">is_callable()</a> - Verify that the contents of a variable can be called as a function</span></li> <li class="member">information about the <a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> type</li> <li class="member"> <span class="methodname"><a href="reflectionfunction.invoke.html" class="methodname" rel="rdfs-seeAlso">ReflectionFunction::invoke()</a> - Invokes function</span></li> <li class="member"> <span class="methodname"><a href="reflectionmethod.invoke.html" class="methodname" rel="rdfs-seeAlso">ReflectionMethod::invoke()</a> - Invoke</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.call-user-func-array.html">call_user_func_array</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.create-function.html">create_function</a></div> <div class="up"><a href="ref.funchand.html">Function handling Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>