Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use named function parameters in PHP? [duplicate]

Is it possible in php like in python to have named function parameters? An example use case is:

function foo($username = "", $password = "", $timeout = 10) {  } 

I want to override $timeout:

foo("", "", 3); 

Ugly. I would much rather do:

foo(timeout=3); 
like image 333
Justin Avatar asked Oct 01 '13 22:10

Justin


People also ask

Does PHP support named parameters?

In PHP 8.0, Named Parameters support is added, which means it's now possible to call a function/method by setting the parameters by their name.

Can we have two functions with the same name in PHP?

Note: PHP's interpretation of overloading is different than most object-oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.

Can you have two functions with the same name in a PHP class Why or why not?

Since PHP is a dynamically typed language, this is not possible.

What is $params in PHP?

PHP Parameterized functions They are declared inside the brackets, after the function name. A parameter is a value you pass to a function or strategy. It can be a few value put away in a variable, or a literal value you pass on the fly. They are moreover known as arguments.


2 Answers

PHP 8.0 added support for named arguments with the acceptance of an RFC.

Named arguments are passed by prefixing the value with the parameter name followed by a colon. Using reserved keywords as parameter names is allowed. The parameter name must be an identifier, specifying dynamically is not allowed.

E.g. to pass just the 3rd optional parameter in your example:

foo(timeout: 3); 

Prior to PHP 8 named parameters were not possible in PHP. Technically when you call foo($timeout=3) it is evaluating $timeout=3 first, with a result of 3 and passing that as the first parameter to foo(). And PHP enforces parameter order, so the comparable call would need to be foo("", "", $timeout=3). You have two other options:

  • Have your function take an array as parameter and check the array keys. I personally find this ugly but it works and is readable. Upside is simplicity and it's easy to add new parameters later. Downside is your function is less self-documenting and you won't get much help from IDEs (autocomplete, quick function param lookups, etc.).
  • Set up the function with no parameters and ask PHP for the arguments using func_get_args() or use the ... variable length arguments feature in PHP 5.6+. Based on the number of parameters you can then decide how to treat each. A lot of JQuery functions do something like this. This is easy but can be confusing for those calling your functions because it's also not self-documenting. And your arguments are still not named.
like image 50
Matt S Avatar answered Sep 18 '22 21:09

Matt S


Old question, but here is some more information.

As stated in the other answers, PHP does not directly support named parameters. VBA is another language which has that luxury.

In PHP, you can use a fairly reasonable substitute. PHP’s array handling is particularly good, and you can use an associative array to pass an array of parameters, as follows:

function foo($parms=[]) {     $username = $parms['username'] ?? '…';     $password = $parms['password'] ?? '…';     $timeout  = $parms['timeout'] ?? '…'; }  foo(['timeout'=>10]); 

Note the use of the ?? operator to allow a simple default if the input array element doesn’t exist. Before PHP 7, you would use ?: which is subtly different, but gives the same result in this case. Thanks to @LeeGee for picking this up.

Not as slick, but named parameters have another important role: they allow you to work with a large number of options without an excessively long list of ordered parameters.

An alternative way of writing the function is to use the `extract function:

function foo($parms=[]) {     $username = '…';     $password = '…';     $timeout = '…';     extract($parms,EXTR_IF_EXISTS); } 

Here the extract function copies the values into the variables, but only if they have already been defined. This prevents importing variables you didn’t know about.

like image 35
Manngo Avatar answered Sep 21 '22 21:09

Manngo