When trying to call a function in a child class with an arbitrary set of parameters, I'm having the following problem:
class Base{
function callDerived($method,$params){
call_user_func_array(array($this,$method),$params);
}
}
class Derived extends Base{
function test($foo,$bar){
print "foo=$foo, bar=$bar\n";
}
}
$d = new Derived();
$d->callDerived('test',array('bar'=>'2','foo'=>1));
Outputs:
foo=2, bar=1
Which... is not exactly what I wanted - is there a way to achieve this beyond re-composing the array with the index order of func_get_args? And yes, of course, I could simply pass the whole array and deal with it in the function... but that's not what I want to do.
Thanks
interface
s, if you want to ensure a specific order. – Hydrouscall_user_func_array()
:call_user_func('func', $array['foo'], $array['bar']);
? – Hydrous...
) to your code. https://mcmap.net/q/348539/-php-default-function-parameter-values-how-to-39-pass-default-value-39-for-39-not-last-39-parameters-duplicate – Ctesiphoncall_user_func_array()
, but the earliest asked question on the topic of "named parameters" was in 2009 and contains a comprehensive/generous answer posted 2020-11-25 (the day before the new feature was deployed with PHP8.0). I consider this page to be the "canonical" on Stack Overflow. – Ostrowski