This RFC proposes allowing trailing commas for all list syntax.
Per the feedback on the internals list, this RFC broadens the scope of the original RFC to allow trailing commas in function arguments to all list syntax.
Arrays in PHP have long since supported trailing commas.
$foo = [ 'foo', 'bar', ];
This makes for clean diffs and easy appending of new values in user-land.
Unfortunately, the other lists do not share the same luxury.
<?php use Foo\Bar\{ Foo, Bar, Baz, };
This RFC proposes allowing trailing commas in all list syntax in order to:
The following lists would allow trailing commas:
Marcio Almada posted a gist with examples of trailing commas for the various lists (shown below):
<?php // Grouped namepaces use Foo\Bar\{ Foo, Bar, Baz, }; // Arrays (already possible) $array = [1, 2, 3,]; // Function/method arguments (call) fooCall($arg1, $arg2, $arg3,); class Foo implements // Interface implementations on a class FooInterface, BarInterface, BazInterface, { // Trait implementations on a class use FooTrait, BarTrait, BazTrait, ; // Class member lists const A = 1010, B = 1021, C = 1032, D = 1043, ; protected $a = 'foo', $b = 'bar', $c = 'baz', ; private $blah, ; // Function/method arguments (declaration) function something(FooBarBazInterface $in, FooBarBazInterface $out,) : bool { } } // Inheriting variables from the parent scope in anonymous functions $foo = function ($bar) use ( $a, $b, $c, ) { // . . . };
There are a number of questions that have already been discussed on the internals list.
PHP allows for multiple splats in one call so trailing commas would work the same way.
foo( ...$args, ...$moreArgs, ...$evenMoreArgs, );
The actual implementation for adding tailing commas to function arguments/calls is two lines. Implementing the functionality to all lists would not require many more changes to the php-src codebase.
function foo( $bar ,$baz ,$boo ) { ... }
TL;DR:
This change would have no breaking changes.
PHP 7.2
Each trailing comma list syntax has its own vote and requires a 2/3 majority to pass.
// Function/method arguments (call) fooCall( $arg1, $arg2, $arg3, ); // Function/method arguments (declaration) function something( FooBarBazInterface $in, FooBarBazInterface $out, ) : bool { }
<?php use Foo\Bar\{ Foo, Bar, Baz, };
class Foo implements FooInterface, BarInterface, BazInterface, { }
class Foo { use FooTrait, BarTrait, BazTrait, ; }
class Foo { const A = 1010, B = 1021, C = 1032, D = 1043, ; protected $a = 'foo', $b = 'bar', $c = 'baz', ; private $blah, ; }
$foo = function ($bar) use ( $a, $b, $c, ) { // . . . };