Join 25,000+ Laravel Developers and join the free Laravel Newsletter
PHP 7.3: Trailing Commas in Function Calls
News / June 14, 2018

PHP 7.3: Trailing Commas in Function Calls

Well PHP 7.3 won’t have arrow functions (that would be dreamy). However, trailing commas in function calls is an excellent addition coming to PHP 7.3.

In PHP 7.3, trailing commas in function calls will be valid syntax. That is to say, you can use trailing commas when calling functions, but not defining them.

Trailing commas in function calls are specifically useful for variadic functions:

// 
unset(
    $one,
    $two,
    $three,
);

return view('posts.show', compact(
    'post',
    'author',
    'comments',
));

Importantly, this change only affects call syntax, not function declaration! The following syntax is still illegal in PHP 7.3:

// Still illegal syntax
public function __construct(
    $apiClient,
    array $options = [],
) {

}

Trailing Commas in PHP Arrays

Using trailing (sometimes referred to as dangling) commas is nice and clean when you want to rearrange an array or append new values.

The trailing comma in PHP arrays has been legal forever (I am not sure when they were introduced) and might be your only exposure to trailing commas in PHP apart from making it legal in grouped namespaces since PHP 7.2.

One argument of why trailing commas are beneficial, and why some teams have been enforcing this code style in arrays, is cleaner diffs. When you want to append a new array item, when you add a comma to the last line to append another value, the diff now affects both lines:

// Before
$points = [
    1,
    5,
    7
];

 // After
 $points = [
     1,
     5,
+    7,
+    9
 ];

With the trailing comma, only the appended line shows up in the diff, nice an clean!

 // Ahh better
 $points = [
     1,
     5,
     7,
+    9,
 ];

Trailing Commas in Other languages

JavaScript

Trailing commas in JavaScript array literals have been allowed since the beginning. Starting in ECMAScript 5, trailing commas in object literals are legal, and in ECMAScript 2017 trailing commas in function parameter lists are legal:

var numbers = [
  1,
  2,
  3,
];

// ECMAScript 5
var stats = {
    rebounds: 9,
    fouls: 2,
    points: 15,
};

// ECMAScript 2017
function track(
  eventName,
  eventValue,
) {
  // ...
}

In JavaScript trailing commas are also allowed in destructuring assignments:

var {
  rebounds,
  points,
  assists,
} = stats; 

Ruby

Here’s an example of a trailing comma on a hash and an array:

ruby_hash = {
    first: '1st',
    second: '2nd',
    third: '3rd',
}

ruby_array = [
    '1st',
    '2nd',
    '3rd',
]

Python

The Python examples look identical to ruby:

countries = [
    'China',
    'India',
    'Mexico',
]

An interesting tidbit, if you were to omit the trailing comma, and accidentally add another item to the array, here’s what the result would be:

countries = [
    'China',
    'India'
]

// Later you added a new item but forgot the comma
countries = [
    'China',
    'India'
    'Mexico'
]

// The result would be...
['China', 'IndiaMexico']

Haskell

Haskell is known for its style of the “trailing comma” called the “Haskell style”:

data Settings = -- The user settings
    { has_sound     :: Bool
    , has_power     :: Bool
    , has_graphics  :: Bool
    , user_name     :: String
    , user_password :: String
    , user_email    :: Email
    , stylesheet    :: Style
    }

The example comes from this StackOverflow answer; and no, I don’t know Haskell.

Because trailing commas can cause cross-browser issues in JavaScript, you might have seen a similar style to this in JavaScript as well:

var stats =
    { rebounds: 9
    , fouls: 2
    , points: 15
    };

Learn More

You can learn all about the most prominent features coming to PHP 7.3 in our post announcing the PHP 7.3 Alpha 1 release last week. PHP 7.3 will hit General availability (GA) later this year!

Also, read through the RFC for allowing a trailing comma in function calls for full details on the proposal coming to PHP 7.3.

The author Sammy Kaye Powers did an excellent job conveying the proposal. If you read carefully, you can learn about how this RFC communicates the difference between this 7.3 proposal and a previously rejected 7.2 version. The 7.2 RFC vote combined both function calls and definitions in a single vote and was narrowly rejected.

This appeared first on Laravel News
Laravel News Partners

Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.