Skip to content

Add func_get_named_args() #19329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Contributor

I'm not entirely sure this would require a RFC. That's why I'm proposing this PR first, and I'll gladly write an RFC if someone requires it.

Allows to write the following code:

<?php

function hello(string $name, int $age, ...$args): array {
  return func_get_named_args();
}

var_dump(hello('World', 30, 'extra1', 'extra2'));

Output:

array(4) {
  ["name"]=>
  string(5) "World"
  ["age"]=>
  int(30)
  [2]=>
  string(6) "extra1"
  [3]=>
  string(6) "extra2"
}

I think this would be a nice addition. With named arguments being more and more popular and the possibility to leverage arrays spreading operator, adding this function feels natural. There's no easy way to do so currently without using reflection.

They differ a bit on they behavior with spread operator and variadic argument:

function hello(string $name, int $age, ...$args): array {
  return func_get_args();
}

var_dump(hello(...[
    'age' => 30,
    'name' => 'John',
    'extra' => 'data',
]));

/*
array(2) {
  [0]=>
  string(4) "John"
  [1]=>
  int(30)
}

-> 'extra' is missing in this output
*/

function hello_named(string $name, int $age, ...$args): array {
  return func_get_named_args();
}

var_dump(hello_named(...[
    'age' => 30,
    'name' => 'John',
    'extra' => 'data',
]));

/*
array(3) {
  ["name"]=>
  string(4) "John"
  ["age"]=>
  int(30)
  ["extra"]=>
  string(4) "data"
}
*/

Adding this function instead of updating func_get_args() seems better to me, which avoids any breaking change.

@iluuu1994
Copy link
Member

Not too much of a fan of further expanding this kind of argument handling, but just my personal opinion. The name sounds a bit ambiguous due to "named args", makes it sound like only arguments that were passed by name would be returned. Maybe func_get_args_by_name() would be less ambiguous.

@alexandre-daubois
Copy link
Contributor Author

An additional use case is that we often use this args handling in Symfony to add new args to methods without breaking backward compatibility between minor versions.

About the naming: your proposition would also suit me fine.

@alexandre-daubois alexandre-daubois force-pushed the func-get-named-args branch 3 times, most recently from 7c08226 to 4a29d16 Compare July 31, 2025 11:54
@alexandre-daubois alexandre-daubois marked this pull request as ready for review July 31, 2025 14:10
@nielsdos
Copy link
Member

Slightly related to #11517 (patch also included)

@NickSdot
Copy link
Contributor

NickSdot commented Aug 1, 2025

I'd love to see this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants