PHP - Fonction call_user_func ()

La fonction call_user_func () peut appeler une fonction utilisateur donnée par le premier paramètre.

Syntaxe

mixed call_user_func(callback function [, mixed parameter [, mixed ...]])

La fonction call_user_func () peut appeler une fonction définie par l'utilisateur donnée par le paramètre "function".

Exemple 1

<?php
    $func = "str_replace";
    $output_single = call_user_func($func, "monkeys", "giraffes", "Hundreds and thousands of monkeys\n");
    echo $output_single;
?>

Production

Hundreds and thousands of giraffes

Exemple 2

<?php
   error_reporting(E_ALL);
   function increment(&$var) {
      $var++;
   }
 
   $a = 0;
   call_user_func("increment", $a);
   echo $a."\n";
?>

Production

0

Exemple 3

<?php 
   function func($a, $b){
      echo $a."\r\n";
      echo $b."\r\n";
   }
 
   call_user_func("func", 1, 2); // The first one is the function name, followed by the parameter list
?>

Production

1
2