PHP - call_user_func_array ()

La fonction call_user_func_array () appelle une fonction utilisateur fournie avec un tableau de paramètres.

Syntaxe

mixed call_user_func_array( callback function [, array param_arr])

La fonction call_user_func_array () peut appeler une fonction personnalisée "function" avec les paramètres de "param_arr array".

Exemple 1

<?php
    $func = "str_replace";
    $params = array("monkeys", "giraffes", "Hundreds and thousands of monkeys\n");
    $output_array = call_user_func_array($func, $params);
    echo $output_array;
?>

Production

Hundreds and thousands of giraffes

Exemple 2

<?php
   function Box($width,$height, $depth) {
      $b = $width*$height*$depth;
      echo $b;
   }
   call_user_func_array("Box", array("width" => 10, "height" => 20, "depth" => 30));
?>

Production

6000

Exemple 3

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

Production

1

Exemple 4

<?php 
   function func($a, $b){
      echo $a."\r\n";
      echo $b."\r\n";
   }
 
   call_user_func_array("func", array(3, 4)); // Different from call_user_func, only the way the parameters are passed is different
?>

Production

3
4