functional-php/named-parameters

Named parameters for functions, methods and constructors.

0.0.2 2019-03-18 16:33 UTC

This package is auto-updated.

Last update: 2024-10-19 10:29:12 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Average time to resolve an issue Percentage of issues still open Chat on Gitter

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

Inspiration