<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.funchand.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.call-user-func-array.php',
    1 => 'call_user_func_array',
    2 => 'Call a callback with an array of parameters',
  ),
  'up' => 
  array (
    0 => 'ref.funchand.php',
    1 => 'Function handling Functions',
  ),
  'prev' => 
  array (
    0 => 'function.call-user-func.php',
    1 => 'call_user_func',
  ),
  'next' => 
  array (
    0 => 'function.create-function.php',
    1 => 'create_function',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/funchand/functions/call-user-func-array.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.call-user-func-array" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">call_user_func_array</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">call_user_func_array</span> &mdash; <span class="dc-title">Call a callback with an array of parameters</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.call-user-func-array-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>call_user_func_array</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$args</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   Calls the <code class="parameter">callback</code> given by the first parameter with
   the parameters in <code class="parameter">args</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.call-user-func-array-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">callback</code></dt>
     <dd>
      <p class="para">
       The <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> to be called.
      </p>
     </dd>
    
    
     <dt><code class="parameter">args</code></dt>
     <dd>
      <p class="para">
       The parameters to be passed to the callback, as an array.
      </p>
      <p class="para">
        If the keys of <code class="parameter">args</code> are all numeric,
        the keys are ignored and each element will be passed to
        <code class="parameter">callback</code> as a positional argument, in
        order.
      </p>
      <p class="para">
        If any keys of <code class="parameter">args</code> are strings,
        those elements will be passed to <code class="parameter">callback</code>
        as named arguments, with the name given by the key.
      </p>
      <p class="para">
        It is a fatal error to have a numeric key in <code class="parameter">args</code>
        appear after a string key, or to have a string key that does not
        match the name of any parameter of <code class="parameter">callback</code>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.call-user-func-array-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the return value of the callback, or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on error.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.call-user-func-array-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>8.0.0</td>
       <td>
        <code class="parameter">args</code> keys will now be interpreted as parameter names, instead of being silently ignored.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.call-user-func-array-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>call_user_func_array()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foobar</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">, </span><span style="color: #0000BB">$arg2</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">__FUNCTION__</span><span style="color: #007700">, </span><span style="color: #DD0000">" got </span><span style="color: #0000BB">$arg</span><span style="color: #DD0000"> and </span><span style="color: #0000BB">$arg2</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br />class </span><span style="color: #0000BB">foo </span><span style="color: #007700">{<br />    function </span><span style="color: #0000BB">bar</span><span style="color: #007700">(</span><span style="color: #0000BB">$arg</span><span style="color: #007700">, </span><span style="color: #0000BB">$arg2</span><span style="color: #007700">) {<br />        echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #DD0000">" got </span><span style="color: #0000BB">$arg</span><span style="color: #DD0000"> and </span><span style="color: #0000BB">$arg2</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br /><br /></span><span style="color: #FF8000">// Call the foobar() function with 2 arguments<br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">, </span><span style="color: #DD0000">"two"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Call the $foo-&gt;bar() method with 2 arguments<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">, </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">), array(</span><span style="color: #DD0000">"three"</span><span style="color: #007700">, </span><span style="color: #DD0000">"four"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">foobar got one and two
foo::bar got three and four</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>call_user_func_array()</strong></span> using namespace name</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">) {<br />        print </span><span style="color: #DD0000">"Hello </span><span style="color: #007700">{</span><span style="color: #0000BB">$name</span><span style="color: #007700">}</span><span style="color: #DD0000">!\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">call_user_func_array</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">, array(</span><span style="color: #DD0000">'Hannes'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">call_user_func_array</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">), array(</span><span style="color: #DD0000">'Philip'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hello Hannes!
Hello Philip!</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Example #3 Using lambda function</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$func </span><span style="color: #007700">= function(</span><span style="color: #0000BB">$arg1</span><span style="color: #007700">, </span><span style="color: #0000BB">$arg2</span><span style="color: #007700">) {<br />    return </span><span style="color: #0000BB">$arg1 </span><span style="color: #007700">* </span><span style="color: #0000BB">$arg2</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$func</span><span style="color: #007700">, array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">int(8)</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-4">
    <p><strong>Example #4 Passing values by reference</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">mega</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">){<br />    </span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">55</span><span style="color: #007700">;<br />    echo </span><span style="color: #DD0000">"function mega \$a=</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= </span><span style="color: #0000BB">77</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'mega'</span><span style="color: #007700">,array(&amp;</span><span style="color: #0000BB">$bar</span><span style="color: #007700">));<br />echo </span><span style="color: #DD0000">"global \$bar=</span><span style="color: #0000BB">$bar</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">function mega $a=55
global $bar=55</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-5">
    <p><strong>Example #5 <span class="function"><strong>call_user_func_array()</strong></span> using named arguments</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foobar</span><span style="color: #007700">(</span><span style="color: #0000BB">$first</span><span style="color: #007700">, </span><span style="color: #0000BB">$second</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">__FUNCTION__</span><span style="color: #007700">, </span><span style="color: #DD0000">" got </span><span style="color: #0000BB">$first</span><span style="color: #DD0000"> and </span><span style="color: #0000BB">$second</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Call the foobar() function with named arguments in non-positional order<br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"second" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"two"</span><span style="color: #007700">, </span><span style="color: #DD0000">"first" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"one"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Call the foobar() function with one named argument<br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">, </span><span style="color: #DD0000">"second" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Fatal error: Cannot use positional argument after named argument<br /></span><span style="color: #0000BB">call_user_func_array</span><span style="color: #007700">(</span><span style="color: #DD0000">"foobar"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"first" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"one"</span><span style="color: #007700">, </span><span style="color: #DD0000">"bar"</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">foobar got one and two
foobar got foo and bar

Fatal error: Uncaught Error: Cannot use positional argument after named argument</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.call-user-func-array-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"><a href="function.call-user-func.php" class="function">call_user_func()</a></span> and <span class="function"><strong>call_user_func_array()</strong></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-array-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.call-user-func.php" class="function" rel="rdfs-seeAlso">call_user_func()</a> - Call the callback given by the first parameter</span></li>
    <li><span class="methodname"><a href="reflectionfunction.invokeargs.php" class="methodname" rel="rdfs-seeAlso">ReflectionFunction::invokeArgs()</a> - Invokes function args</span></li>
    <li><span class="methodname"><a href="reflectionmethod.invokeargs.php" class="methodname" rel="rdfs-seeAlso">ReflectionMethod::invokeArgs()</a> - Invoke args</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>