Named arguments in functions, default and nullable values in PHP

Video thumbnail

Here I wanted to show you a little about the use of named arguments in PHP, in case you're unfamiliar with it. It's in these types of situations that you can tell PHP is a language that's been around for years. Something similar happened with Java when we compared it to Kotlin. As soon as Kotlin was born, it already had modern features that Java didn't have: for example, null pointer exception handling and, precisely, named arguments, which is what I want to talk about here.

Classical approach vs. named arguments

What we see first is the classic, time-tested approach, which we can use in virtually any programming language. It simply involves defining a function—in this case, a static function—and passing it a list of parameters:

public static function inscribe($book, $ordenId, $trace, $price,$user, $coupon, $payment,$visibility, $type,$affiliateUser)

We use it:

  $codeMsj = Book::inscribe(
             $book,
             $orderId,
             $trace,
             $pricePayed,
             $user,
             $coupon,
             $payment,
             $visibility,
             $type,
             $affiliateUser
        );

Yes, I know Clean Code says we shouldn't pass more than three or four parameters... but hey, that's what my function needs to work, and I don't know how to do it any other way. So to each their own.

Problems of the classical approach

What do I mean by this? It's prone to errors, such as mixing up parameters. For example, passing the wrong value to the type parameter, or assigning random values, causing confusion when reading the function.

Of course, there's a second useful tip: declaring the types. This is optional in PHP, but highly recommended:

public static function inscribe(Book $book, string $ordenId, string $trace, $price, User $user, string $coupon, string $payment, string $visibility, string $type, AffiliateUser $affiliateUser)

You could, for example, define that a parameter can be an int or a string using the pipe |. In this case, it doesn't apply because the function I'm showing always expects a string, since it's a function to register a product.

Data types

For me, a well-declared function starts with specifying the data types whenever possible. Even when you're not 100% sure what data will be received, you can use values like null or declare multiple types:

public static function inscribe(Book $book, string $ordenId, string $trace, $price, User $user, ?string $coupon, string $payment, string $visibility, string $type, ?AffiliateUser $affiliateUser = null)

And this is where what I want to show you comes in: the use of named arguments, which greatly improves the clarity of the code.

Syntax of named arguments

If you're coming from a modern language like Kotlin, this feature will be familiar. In PHP, you simply use the argument name followed by a colon : and then the value.

For example, if the argument is called book, then you would name it like this:

$codeMsj = Book::inscribe(
    book: $book,
    ordenId: $orderId,
    trace: $trace,
    price: $pricePayed,
    user: $user,
    coupon: $coupon,
    payment: $payment,
    visibility: $visibility,
    type: $type,
    affiliateUser: $affiliateUser
);

This makes your intent clearer and prevents errors. If, for example, you misspell it (boo instead of book), you'll immediately get an error. Plus, you can pass the arguments in any order you want, as long as you use this syntax.

Another advantage is that you can assign default values, such as null, which also helps avoid null pointer exception errors:

?AffiliateUser $affiliateUser = null

With named arguments, you can pass as many parameters as you need without fear of making mistakes. It's a clearer, safer, and more maintainable way to write functions.

I agree to receive announcements of interest about this Blog.

We talked about how we can use functions with named arguments in functions, default values and nullable in PHP.

| 👤 Andrés Cruz

🇪🇸 En español