Dear Internals,
I would like to as you a question. I know that it is possible to use an
array to function parameters like: function(...array); But this method is
not working when the array is associative.
Would not be fine if an array like ['param1'=>'value', 'param2' => 'value']
would be used in a function like function(param1, param2) regardless of the
order?
I think it would make our lives is easier. I'd be also happy if you know if
there is more effective method to achieve the same result.
Thanks,
Midori
I might be off here, but I believe the issue with that is that it's a
performance problem. You would have to check each key and function
parameter for a match, which would be slow.
You can, however, achieve what you're after just by using an array without
any packing/unpacking.
function foo(Array $args) {
// Expected arguments
$default = ["name" => null, "birthday" => null];
// Get actual arguments
$input = array_intersect_key($args, $default) + $default; // final
if (!isset($input['name'])) {
// ohnoes you forgot your name
}
if (!isset($input['birthday'])) {
// ohnoes you forgot your birthday
}
// otherwise use $Input['name'] and $input['birthday'] here...
}
Dear Internals,
I would like to as you a question. I know that it is possible to use an
array to function parameters like: function(...array); But this method is
not working when the array is associative.Would not be fine if an array like ['param1'=>'value', 'param2' => 'value']
would be used in a function like function(param1, param2) regardless of the
order?I think it would make our lives is easier. I'd be also happy if you know if
there is more effective method to achieve the same result.Thanks,
Midori
Dear Internals,
I would like to as you a question. I know that it is possible to use an
array to function parameters like: function(...array); But this method is
not working when the array is associative.Would not be fine if an array like ['param1'=>'value', 'param2' => 'value']
would be used in a function like function(param1, param2) regardless of the
order?I think it would make our lives is easier. I'd be also happy if you know if
there is more effective method to achieve the same result.Thanks,
Midori
What you are looking for are Named Parameters, there have been multiple
RFCs about which have all been more or less abandoned from what I see.
https://wiki.php.net/rfc/namedparameters (From 2012)
https://wiki.php.net/rfc/named_params (From 2013)
https://wiki.php.net/rfc/simplified_named_params (From 2016)
On a side note, I don't think this feature is that useful and if you get
to the
point where you want to know which value you are passing to which argument
it seems, to me, that the function is having too many arguments.
Also IDEs exist and can tell you the name of the argument.
Best regards
George P. Banyard
Dear Internals,
I would like to as you a question. I know that it is possible to use an
array to function parameters like: function(...array); But this method is
not working when the array is associative.Would not be fine if an array like ['param1'=>'value', 'param2' => 'value']
would be used in a function like function(param1, param2) regardless of the
order?I think it would make our lives is easier. I'd be also happy if you know if
there is more effective method to achieve the same result.Thanks,
MidoriWhat you are looking for are Named Parameters, there have been multiple
RFCs about which have all been more or less abandoned from what I see.
https://wiki.php.net/rfc/namedparameters (From 2012)
https://wiki.php.net/rfc/named_params (From 2013)
https://wiki.php.net/rfc/simplified_named_params (From 2016)
Can anyone summarize why these did not move forward?
Nikita's proposal sounds pretty good IMO, and I think the follow up proposal would have been nice, too. Any port in a storm!
On a side note, I don't think this feature is that useful and if you get
to the point where you want to know which value you are passing to
which argument it seems, to me, that the function is having too many
arguments. Also IDEs exist and can tell you the name of the argument.
I would assert a counter to your assertion that this feature would not be that useful, at least several known use-cases.
The known use-cases include:
- Querying, be it SQL or some other query language.
- Wrapping API calls (which is a form of querying.)
- Instantiating properties of an object via __construct().
I assume most on this list are not fans of WordPress but that notwithstanding WP_Query() is a good example of a class that accepts an array of arguments on instantiation:
Unfortunately PHP can't type check those WP_Query() arguments, but it could type check named parameters of a query() method.
Yes, you can model all arguments as properties of an object, but then how would you instantiate that class? Named properties would make doing so much less tedious and allow it to be done in a single expression rather than requiring multiple assignment statements.
-Mike
P.S. This would be on my top 5 list of features I would like to see added in PHP 8. FWIW.
What you are looking for are Named Parameters, there have been multiple
RFCs about which have all been more or less abandoned from what I see.
https://wiki.php.net/rfc/namedparameters (From 2012)
https://wiki.php.net/rfc/named_params (From 2013)
https://wiki.php.net/rfc/simplified_named_params (From 2016)
Can anyone summarize why these did not move forward?
Without digging back into the archives, my memory is that points of
discussion included:
-
The choice of syntax.
-
Support for internal functions, which don't declare parameters in the
same way. I believe this is now solved, or at least vastly improved, by
providing reflection information for internal functions via PHP-syntax
stubs (see https://externals.io/message/105851 and
https://externals.io/message/105970). -
Handling of inheritance: what happens if a class overrides a base
method (or implements an interface method) but uses different parameter
names? If all parameters are immediately accessible by name (i.e. no
extra opt-in is needed in the definition), there will be a large amount
of existing code which would immediately break LSP this way. -
Mixing named and positional parameters in a single function call.
-
Handling of variadics, both the function foo(...$args) syntax and the
specialfunc_get_args()
etc functions -
Behaviour when a non-optional parameter is skipped (all three RFCs
predate https://wiki.php.net/rfc/too_few_args)
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]