It is often useful to be able to call a function or methods using named parameters. For example to only pass a different value for the nth default parameter.
PHP does not allow to do that, this library ought to change that.
composer require functional-php/named-parameters
Say you have the following function:
function return_array($a, $b, $c) {
return [$a, $b, $c];
}
Any of the following call is equivalent:
use function FunctionalPHP\NamedParameters\call_user_func_array_np;
// traditional call with positional arguments
call_user_func_array_np('return_array', [1, 2, 3]);
// named arguments in the correct order
call_user_func_array_np('return_array', ['a' => 1, 'b' => 2, 'c' => 3]);
// named arguments in random order
call_user_func_array_np('return_array', ['c' => 3, 'a' => 1, 'b' => 2]);
call_user_func_array_np('return_array', ['c' => 3, 'b' => 2, 'a' => 1]);
You can run the test suite for the library using:
composer test
A test report will be available in the reports
directory.
Any contribution welcome :
- Ideas
- Pull requests
- Issues