<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.call-user-func.php',
    1 => 'call_user_func',
    2 => 'Appelle une fonction de rappel fournie par le premier argument',
  ),
  'up' => 
  array (
    0 => 'ref.funchand.php',
    1 => 'Fonctions sur la gestion des fonctions',
  ),
  'prev' => 
  array (
    0 => 'ref.funchand.php',
    1 => 'Fonctions sur la gestion des fonctions',
  ),
  'next' => 
  array (
    0 => 'function.call-user-func-array.php',
    1 => 'call_user_func_array',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/funchand/functions/call-user-func.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" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">call_user_func</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">call_user_func</span> &mdash; <span class="dc-title">Appelle une fonction de rappel fournie par le premier argument</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="methodname"><strong>call_user_func</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.mixed.php" class="type mixed">mixed</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">
   Appelle la fonction de rappel <code class="parameter">callback</code>
   fournie par le premier paramètre, avec les autres arguments passés en tant que paramètres.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.call-user-func-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">callback</code></dt>
     <dd>
      <p class="para">
       La fonction de rappel à appeler.
      </p>
     </dd>
    
    
     <dt><code class="parameter">args</code></dt>
     <dd>
      <p class="para">
       <code class="literal">0</code> ou plus de paramètres à passer à la fonction de rappel.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        Il est à noter que les paramètres pour <span class="function"><strong>call_user_func()</strong></span> ne sont
        pas passés par référence.
        <div class="example" id="example-1">
         <p><strong>Exemple #1 Exemple avec <span class="function"><strong>call_user_func()</strong></span> par référence</strong></p>
         <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">(&amp;</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: #FF8000">// il est possible d'utiliser ceci à la place<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(&amp;</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: #FF8000">// il est aussi possible d'utiliser une fonction variable<br /></span><span style="color: #0000BB">$increment </span><span style="color: #007700">= </span><span style="color: #DD0000">'increment'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$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 /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
         </div>

         <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
         <div class="example-contents screen">
<div class="parameterscode"><pre class="parameterscode">Warning: Parameter 1 to increment() expected to be a reference, value given in …
0
1
2</pre>
</div>
         </div>
        </div>
       </p>
      </p></blockquote>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.call-user-func-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne la valeur retournée par la fonction de rappel.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.call-user-func-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Exemple avec <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">&lt;?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">"Vous voulez une coupe </span><span style="color: #0000BB">$type</span><span style="color: #DD0000">, aucun problème"</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">"au bol"</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">"au rasoir"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Vous voulez une coupe au bol, aucun problème
Vous voulez une coupe au rasoir, aucun problème</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Exemple avec <span class="function"><strong>call_user_func()</strong></span> en utilisant un espace de noms</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">() {<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">);<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">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hello world!
Hello world!</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-4">
    <p><strong>Exemple #4 Utilisation d&#039;une méthode de classe avec <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">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">maclasse </span><span style="color: #007700">{<br />    static function </span><span style="color: #0000BB">dit_bonjour</span><span style="color: #007700">()<br />    {<br />        echo </span><span style="color: #DD0000">"Bonjour!\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">"maclasse"</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">'dit_bonjour'</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">'::dit_bonjour'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$monobjet </span><span style="color: #007700">= new </span><span style="color: #0000BB">maclasse</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">$monobjet</span><span style="color: #007700">, </span><span style="color: #DD0000">'dit_bonjour'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Bonjour!
Bonjour!
Bonjour!</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-5">
    <p><strong>Exemple #5 Utilisation d&#039;une fonction lambda avec <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">&lt;?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">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">[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">
 Notez que les fonctions de rappel enregistrées avec des fonctions comme <span class="function"><strong>call_user_func()</strong></span> et
 <span class="function"><a href="function.call-user-func-array.php" class="function">call_user_func_array()</a></span> ne seront pas appelées si une exception n&#039;est pas interceptée alors
 qu&#039;elle a été lancée dans une précédente fonction de rappel.
</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.call-user-func-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.call-user-func-array.php" class="function" rel="rdfs-seeAlso">call_user_func_array()</a> - Appelle une fonction de rappel avec les param&egrave;tres rassembl&eacute;s en tableau</span></li>
    <li><span class="function"><a href="function.is-callable.php" class="function" rel="rdfs-seeAlso">is_callable()</a> - D&eacute;termine si une valeur peut &ecirc;tre appel&eacute; comme une fonction
   dans la port&eacute;e courante</span></li>
    <li><a href="functions.variable-functions.php" class="link">Fonctions variables</a></li>
    <li><span class="methodname"><a href="reflectionfunction.invoke.php" class="methodname" rel="rdfs-seeAlso">ReflectionFunction::invoke()</a> - Invoque une fonction</span></li>
    <li><span class="methodname"><a href="reflectionmethod.invoke.php" class="methodname" rel="rdfs-seeAlso">ReflectionMethod::invoke()</a> - Invoque</span></li>
   </ul>
  </p>
 </div>


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