Named Arguments in PHP 8: The Little Feature That Could

Nemanja Milenkovic
4 min readMar 29, 2023

--

Imagine you’re attending a fancy dinner party, and the host has prepared a scrumptious meal. You dig in and quickly realize that the ingredients are playing hide-and-seek with your taste buds. “What could that mystery ingredient be?” you wonder as your culinary curiosity takes over. This culinary conundrum is a lot like working with PHP functions before PHP 8.

Before PHP 8, passing arguments to functions often required code-sleuthing to understand the purpose of each argument. But fear not, dear reader! The release of PHP 8 brought us named arguments, a feature that’s as tasty as a pinch of saffron in a risotto. Let’s explore the world of named arguments in PHP and learn how they can spice up your code.

Pass the Salt, Please: A Quick Introduction to Named Arguments

So, what’s the big deal with named arguments? Named arguments allow you to pass values to a function by explicitly specifying the parameter names. This means you can skip the guessing game when reading a function call and know exactly which value corresponds to which parameter.

Picture this: You’re reading some PHP code and stumble upon a function call like cook_meal(3600, 180, 45);. You might wonder, "What on Earth do these numbers mean? Are they ingredients or cooking times?" Then you check the function definition and find out that the parameters are $cookingTime, $prepTime, and $coolingTime. If only there was a better way...

Enter named arguments! With PHP 8, you can now rewrite the function call like this: cook_meal(cookingTime: 3600, prepTime: 180, coolingTime: 45);. Voilà! No more head-scratching or code-digging necessary. It's crystal clear which value corresponds to which parameter. Your code is now as easy to read as a recipe card.

Juggling Arguments Like a Pro: Mixing and Matching Named and Positional Arguments

One of the coolest things about named arguments is that they can be used alongside traditional positional arguments. This means you can mix and match to your heart’s content, just like experimenting with flavors in the kitchen.

Let’s say you have a function called make_cocktail($base, $mixer, $garnish). You can pass arguments in any order you want, as long as you use named arguments for those that are out of order. For example, you could call the function like this: make_cocktail('gin', garnish: 'lime', mixer: 'tonic');. It's like creating a signature cocktail with your own unique twist!

A Pinch of Salt: Optional Parameters and Default Values

Named arguments also make dealing with optional parameters a piece of cake (or a slice of key lime pie, if you prefer). If a function has optional parameters with default values, you can choose to override only the ones you need. No more passing null or copying default values from the function definition!

Let’s take a look at our make_cocktail function again. Suppose the $garnish parameter is optional and defaults to an 'olive'. If you want a gin and tonic with a twist of lemon instead, simply call the function like this: make_cocktail('gin', 'tonic', garnish: 'lemon');. Your code stays clean and dry, just like a good martini.

A Word of Caution: Spilling the Salt

Before you dive into the world of named arguments like a kid in a candy store, be aware that they come with a caveat: Using named arguments makes your code dependent on the parameter names of the functions you’re calling. If the parameter names change in the future, your code could break like a dropped soufflé. To avoid this, it’s best to use named arguments with well-established functions or your own code where you have control over the parameter names.

TL;DR: Serving Up Tasty Code with Named Arguments

Named arguments in PHP 8 bring a whole new level of readability and flexibility to your code. They allow you to:

  • Pass values to functions by specifying parameter names, making your code as clear as a consommé.
  • Mix and match named and positional arguments, giving you the freedom to create the perfect blend of code readability and conciseness.
  • Easily work with optional parameters and default values, so you don’t have to pass null or copy default values from function definitions.

But remember to be cautious when using named arguments with functions whose parameter names might change, lest your code falls flat like an overbeaten meringue.

In conclusion, named arguments are like the secret spice that elevates a dish from ordinary to extraordinary. So go ahead, experiment with named arguments in your PHP projects, and give your code the flavor boost it deserves!

--

--

Nemanja Milenkovic

Web dev expert with 20+ yrs experience. LAMP technologies maestro, adept in PHP, Laravel, Symfony...