PHP 7.3 Trailing commas in function calls

PHP 7.3 Trailing commas in function calls

Void return types in PHP 7.1

The ability to have a trailing comma in arrays is already built-in, so why not have it extended to function/method parameters?

This will easily allow us to pass a dynamic list of arguments created on the fly without many problems. It will also grant the possibility to keep that last comma, like it does for arrays when we list arguments for function per lines, below. Of course leading commas and multiple commas still won’t be allowed and will cause the syntax error. You cannot use more than one comma at the end or use commas to skip arguments.

//new allowed cases in build-in function and..
unset(
    $foo,
    $bar,
    $baz,
);

array_merge(
    $array1,
    $array2,
    ['one', two'],
);

We can also leave a trailing comas in method calls directly too

$object->methodName(
  'first',
  'second',
);

Reference

PHP 7 News & Updates v7.0 - 7.4 - Book coverPHP 7 News & Updates v7.0 – 7.4 Book https://www.amazon.com/PHP-News-Updates-v7-0-7-4/dp/1727202481/

Rate this artcile
[Total: 2 Average: 4.5]
Please follow and like us:

Leave a Comment