functional-php / named-parameters
Named parameters for functions, methods and constructors.
Installs: 4 766
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=7.1.0
Requires (Dev)
- atoum/atoum: *
This package is auto-updated.
Last update: 2024-10-19 10:29:12 UTC
README
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.
Installation
composer require functional-php/named-parameters
Basic Usage
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]);
Testing
You can run the test suite for the library using:
composer test
A test report will be available in the reports
directory.
Contributing
Any contribution welcome :
- Ideas
- Pull requests
- Issues