Calling Functions Dynamically

$fn() calls the function whose name $fn holds, and $obj->$method() a method. call_user_func() and call_user_func_array() do the same, and since PHP 8.0 string keys in the argument array act as named arguments, as they do with .... function_exists(), method_exists() and get_defined_functions() report what exists.

A variable function behind an allowlist, and arguments passed by namePHP
<?php
declare(strict_types=1);
function report_daily(string $d): string { return "daily sales for $d"; }
function report_weekly(string $d): string { return "weekly sales from $d"; }
$type = 'weekly';                             // imagine it came from $_GET['type']
$fn = match ($type) {                         // an allowlist, never the raw input
  'daily', 'weekly' => "report_$type",
  default => throw new InvalidArgumentException("no report '$type'"),
};
echo $fn('2026-09-21'), ' | ', implode(',', get_defined_functions()['user']), "\n";
$args = ['string' => '7', 'length' => 3, 'pad_type' => STR_PAD_LEFT, 'pad_string' => '0'];
echo call_user_func_array('str_pad', $args), ' ', str_pad(...$args), "\n";
Output
weekly sales from 2026-09-21 | report_daily,report_weekly
007 007

Never call a name taken from request data without an allowlist: $_GET['f']() lets a visitor run system or unlink. In new code, $fn(...$args) replaces call_user_func_array().