Re: [Proposal] call_user_func_map(): flexible named parameter mapping for call_user_func*

From: Date: Thu, 01 Jul 2021 16:30:52 +0000
Subject: Re: [Proposal] call_user_func_map(): flexible named parameter mapping for call_user_func*
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-115267@lists.php.net to get a copy of this message


> Le 27 juin 2021 à 16:39, Ralph Schindler <ralph@ralphschindler.com> a écrit :
> 
> 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"
> 
> 

Hi,

If you want to ignore arguments in excess, your function may have a rest parameter collecting them.
The following will run without error:

```php
function foo($c, $a, ...$unused) {
    var_dump($a, $c);
}

$args = [
    'a' => 'the a value'
  , 'b' => 'the b value'
  , 'c' => 'the c value'
];

foo(...$args);
```

—Claude


Thread (9 messages)

« previous php.internals (#115267) next »