假设有一个带有一些参数的函数,我有一个关联数组(或者一个简单的带有公共属性的对象——几乎是一样的,因为我可以始终使用类型转换
(object)$array
),其键对应于函数参数名称,值对应于函数调用参数。我该如何调用它并将它们传递进去?<?php
function f($b, $a) { echo "$a$b"; }
// notice that the order of args may differ.
$args = ['a' => 1, 'b' => 2];
call_user_func_array('f', $args); // expected output: 12 ; actual output: 21
f($args); // expected output: 12 ; actual output: ↓
// Fatal error: Uncaught ArgumentCountError:
// Too few arguments to function f(), 1 passed