Share
Preview
With named arguments, you can explicitly name each argument in the function call.
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
New in PHP 8: Named Arguments
by Alex
Named Arguments



This is the first email of a series where I talk about the most interesting new features of PHP 8, the current major PHP version released in November 2020.

Today we look at function Named Arguments.


PHP until version 7 supports positional arguments only.

"Positional" means that, when you call a function, you must write the function's arguments in the correct order.
It's the position of the argument that determines its role innside the function.

(More precisely: the position determines the mapping between the argument and the function's internal variable).

With named arguments, you can explicitly name each argument in the function call.

In this case, it's the name of the argument that determines the mapping with the function's internal variable, instead of its position.


Let's take an example:
$str = substr($myString, 5);

Here, $myString and 5 are the first and second positional arguments respectively.
Therefore, they are mapped to the first and second internal function's variables (which are: $string and $offset).

With named arguments, you can write the same function call like this:
$str = substr(string: $myString, offset: 5);

The string: and offset: identifiers are the variable names from the function definition (that you can find here).

You can write the named arguments in any order, b
ecause the position does not matter:
$str = substr(offset: 5, string: $myString);




Combining positional and named arguments.

You can use positional and named arguments at the same time.
When you do so, the positional arguments must come first, and the named arguments after.


For example:
$time = mktime(4, year: 2010);

The first argument, $hour, is passed using the first positional argument (the value "4").
The other argument, $year, is passed using the named argument "year".

This is useful if you want to skip all the optional arguments between $hour and $year.

With positional arguments only, you'd have to list *all* the optional arguments up to $year, like this:
$time = mktime(4, NULL, NULL, NULL, NULL, 2010);




Named arguments: PROs and CONs


Named arguments have two big advantages.
The first is that you can skip optional arguments, regardless of their position.
This is exactly what happened in the mktime() example from before.

Here is another example: you want to call htmlspecialchars() and set the last argument to false.

With positional arguments you need to set all the arguments:
$html = htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

But with named arguments you can just set the last one:
$html = htmlspecialchars($string, double_encode: false);


The second nice thing about named arguments is that they make the code more readable.
You can see exactly what the arguments of a function call mean, because the parameters' names are visible.

For example:
$found = in_array($a, $b);

Is $a the array where to look into, and $b the item to look for? Or is it the other way around?
Sometimes it can be difficult to understand the arguments' roles.

By using named arguments, this problem is solved:
$found = in_array(needle: $a, haystack: $b);


The main con of named arguments is that the arguments names must be the same in the function definition and in the function call.

Therefore, if you change the name of the arguments in the function definition, the function calls using named arguments will not work anymore.

This can be an issue with libraries and APIs.
Since you cannot control whether people will use named arguments in their calls, with PHP 8 you must try avoiding changing your arguments names as much as possible.




That's all for today.
Now  send me a reply with your questions and let me know what you think. I would love to hear from you.

Until next time,
Alex



Share the knowledge

Do you like this email? Share it with your friends!
Click here to share it.



Premium products to improve your skills

PHP courses

PHP Security Mastery
What if you could write code that is always secure from attacks?
Just imagine how this would increase your reputation and your possibilities as a developer.
That is exactly what you will get from this course.
You will learn to use the defense techniques that really work, leaving nothing to chance (with real code examples).

Take a look and see for yourself.



Resources

Alex Web Develop - My blog with my tutorials.
Alex PHP café
- My Facebook group where to talk with me and other developers.


Image by Freepik - www.freepik.com



You are receiving this newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more emails from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign