The short proposal:
Make a variation of this work (notice parameter order and 'b' is goes
unused):
call_user_func_map(
fn ($c, $a) => var_dump($a, $c),
[
'a' => 'the a value',
'b' => 'the b value',
'c' => 'the c value'
]
);
// string(11) "the a value"
// string(11) "the c value"
The long proposal:
Callables, Closures, Anon Funcs, etc - have enjoyed increasing
popularity year over year. We've even gotten a shorter syntax for them
`fn ()` and more RFC's are sure to be on their way.
This proposes a method for a publisher/framework/producer (the caller of
the callback) to document all the parameters that are available as named
parameters, and subscribers/consumers (creator of the callback) could
subscribe to what they need by name.
For example:
// documented callback parameters, this is what is available:
$params = [
'a' => 'value for a',
'b' => 'value for b',
'c' => 'value for c'
];
// a consumer only interested in $a and $c could write:
$f = fn ($c, $a) => var_dump($a, $c);
The consumer would then register their callback with the
producer/framework/etc or pass as the parameter to whatever is consuming it
$callbackSystem->register('some_event', $f)
// or
$doSomething->with($f);
The framework could then call with success:
call_user_func_map($usersCallable, $allAvailableNamedParameters);
Currently the only way to do this is through array_diffing parameters
found via Reflection, then passing them along to call_user_func_array()
as a named array.
Attempting to use call_user_func_array() without specifying all provided
variables results in:
Uncaught Error: Unknown named parameter X in ...
Thoughts?
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php