Take the State of PHP 2026 Survey →

Trailing commas in list syntax View as Markdown

Implemented (PHP 7.2)
Authors
Sammy Kaye Powers, me@sammyk.me
Date
2015-11-03 (discussion); 2017-01-27 (voting)
Version
0.1
Source
wiki.php.net/rfc/list-syntax-trailing-commas

Votes

Allow trailing commas in function/method arguments (declarations and calls) closed
Yes
24
No
20
44 voters
adambaratz No 2017-01-28 03:52
ashnazg Yes 2017-01-27 19:16
bishop Yes 2017-02-06 17:19
bwoebi No 2017-01-27 16:17
cmb No 2017-01-29 23:45
danack Yes 2017-02-01 01:16
daverandom Yes 2017-01-27 22:39
derick No 2017-01-30 09:46
dmitry No 2017-02-09 07:33
eliw No 2017-02-03 21:01
galvao Yes 2017-01-30 02:20
jhdxr No 2017-01-28 05:25
joey Yes 2017-02-06 22:53
kalle No 2017-01-27 16:41
kelunik Yes 2017-02-01 09:12
keyur Yes 2017-01-27 18:13
kguest Yes 2017-02-02 22:09
laruence No 2017-01-28 03:10
lcobucci No 2017-01-27 22:26
leigh No 2017-01-27 17:01
malukenho No 2017-02-03 20:52
marcio Yes 2017-01-28 00:45
mariano Yes 2017-01-27 18:22
mbeccati No 2017-01-30 11:57
mfonda Yes 2017-01-31 22:02
mike Yes 2017-01-31 10:01
nikic Yes 2017-01-27 16:29
ocramius Yes 2017-01-29 16:27
pajoye Yes 2017-01-30 03:23
pierrick No 2017-01-31 19:36
pmjones No 2017-01-31 17:14
pollita Yes 2017-02-01 01:03
rmf Yes 2017-01-31 21:58
salathe No 2017-02-08 19:51
sammyk Yes 2017-01-27 16:03
sebastian No 2017-01-28 06:07
stas No 2017-01-27 22:00
svpernova09 Yes 2017-02-05 20:12
thekid Yes 2017-01-27 18:29
trowski Yes 2017-01-30 04:38
weierophinney Yes 2017-02-08 17:16
yunosh Yes 2017-01-28 10:04
zeev No 2017-01-30 08:56
zimt No 2017-01-31 15:04
Allow trailing commas in grouped namepaces closed
Yes
30
No
9
39 voters
adambaratz Yes 2017-01-28 03:52
ajf Yes 2017-01-28 15:07
ashnazg Yes 2017-01-27 19:16
bishop Yes 2017-02-06 17:19
bwoebi No 2017-01-27 16:17
colinodell Yes 2017-01-30 00:34
daverandom Yes 2017-01-27 22:39
dmitry No 2017-02-09 07:33
eliw No 2017-02-03 21:02
galvao Yes 2017-01-30 02:20
joey Yes 2017-02-06 22:54
kalle No 2017-01-27 16:42
kelunik Yes 2017-02-01 09:12
keyur Yes 2017-01-27 18:13
kguest Yes 2017-02-02 22:10
lcobucci Yes 2017-01-27 22:26
leigh Yes 2017-01-27 17:02
levim Yes 2017-01-27 16:26
malukenho Yes 2017-02-03 20:53
marcio Yes 2017-01-28 00:44
mariano Yes 2017-01-27 18:22
mbeccati Yes 2017-01-30 11:57
mike Yes 2017-01-31 10:01
nikic Yes 2017-01-27 16:31
ocramius Yes 2017-01-29 16:28
pajoye Yes 2017-01-30 03:24
pierrick No 2017-01-31 19:36
pollita Yes 2017-02-01 01:03
rmf Yes 2017-01-31 21:58
salathe No 2017-02-08 19:51
sammyk Yes 2017-01-27 16:03
sebastian No 2017-01-28 06:07
stas No 2017-01-27 22:02
thekid Yes 2017-01-27 18:30
trowski Yes 2017-01-30 04:36
weierophinney Yes 2017-02-08 17:16
yunosh Yes 2017-01-28 10:05
zeev Yes 2017-01-30 08:57
zimt No 2017-01-31 15:04
Allow trailing commas in interface implementations on a class closed
Yes
8
No
29
37 voters
adambaratz No 2017-01-28 03:52
ajf No 2017-01-28 15:07
ashnazg No 2017-01-27 19:17
bishop Yes 2017-02-06 17:20
bwoebi No 2017-01-27 16:17
cmb No 2017-01-29 23:38
daverandom No 2017-01-27 22:40
eliw No 2017-02-03 21:02
galvao No 2017-01-30 02:21
jhdxr No 2017-01-28 05:26
joey Yes 2017-02-06 22:55
kalle No 2017-01-27 16:42
kelunik No 2017-02-01 09:12
keyur No 2017-01-27 18:13
kguest No 2017-02-02 22:11
lcobucci No 2017-01-27 22:27
leigh No 2017-01-27 17:02
levim No 2017-01-27 16:27
malukenho No 2017-02-03 20:53
marcio Yes 2017-01-28 00:45
mariano No 2017-01-27 18:22
mbeccati No 2017-01-30 11:57
mfonda No 2017-01-31 22:02
mike Yes 2017-01-31 10:01
nikic No 2017-01-27 16:51
ocramius No 2017-01-29 16:28
pajoye No 2017-01-30 03:24
pierrick No 2017-01-31 19:36
pollita Yes 2017-02-01 01:07
salathe No 2017-02-08 19:51
sammyk Yes 2017-01-27 16:03
sebastian No 2017-01-28 06:07
stas No 2017-01-27 22:01
trowski No 2017-01-30 04:38
weierophinney Yes 2017-02-08 17:16
yunosh Yes 2017-01-28 10:05
zeev No 2017-01-30 08:58
Allow trailing commas in trait implementations on a class closed
Yes
7
No
29
36 voters
adambaratz No 2017-01-28 03:52
ajf No 2017-01-28 15:07
ashnazg No 2017-01-27 19:18
bishop Yes 2017-02-06 17:20
bwoebi No 2017-01-27 16:17
cmb No 2017-01-29 23:40
daverandom No 2017-01-27 22:40
eliw No 2017-02-03 21:02
galvao No 2017-01-30 02:21
jhdxr No 2017-01-28 05:26
joey Yes 2017-02-06 22:54
kalle No 2017-01-27 16:42
kelunik No 2017-02-01 09:13
keyur No 2017-01-27 18:12
kguest No 2017-02-02 22:11
lcobucci No 2017-01-27 22:27
leigh No 2017-01-27 17:02
levim No 2017-01-27 16:27
malukenho No 2017-02-03 20:54
marcio Yes 2017-01-28 00:46
mariano No 2017-01-27 18:22
mbeccati No 2017-01-30 11:57
mike Yes 2017-01-31 10:01
nikic No 2017-01-27 16:51
ocramius No 2017-01-29 16:28
pajoye No 2017-01-30 03:25
pierrick No 2017-01-31 19:37
pollita No 2017-02-01 01:06
salathe No 2017-02-08 19:51
sammyk Yes 2017-01-27 16:03
sebastian No 2017-01-28 06:07
stas No 2017-01-27 22:02
trowski No 2017-01-30 04:39
weierophinney Yes 2017-02-08 17:16
yunosh Yes 2017-01-28 10:05
zeev No 2017-01-30 08:59
Allow trailing commas in class member lists closed
Yes
7
No
29
36 voters
adambaratz No 2017-01-28 03:53
ajf No 2017-01-28 15:07
ashnazg No 2017-01-27 19:18
bishop Yes 2017-02-06 17:21
bwoebi No 2017-01-27 16:17
cmb No 2017-01-29 23:40
daverandom No 2017-01-27 22:41
eliw No 2017-02-03 21:02
galvao No 2017-01-30 02:22
jhdxr No 2017-01-28 05:26
joey Yes 2017-02-06 22:55
kalle No 2017-01-27 16:43
kelunik No 2017-02-01 09:13
keyur No 2017-01-27 18:12
kguest No 2017-02-02 22:11
lcobucci No 2017-01-27 22:27
leigh No 2017-01-27 17:02
levim No 2017-01-27 16:26
malukenho No 2017-02-03 20:53
marcio Yes 2017-01-28 00:45
mariano No 2017-01-27 18:23
mbeccati No 2017-01-30 11:57
mike Yes 2017-01-31 10:02
nikic No 2017-01-27 16:56
ocramius No 2017-01-29 16:28
pajoye No 2017-01-30 03:24
pierrick No 2017-01-31 19:37
pollita No 2017-02-01 01:05
salathe No 2017-02-08 19:50
sammyk Yes 2017-01-27 16:03
sebastian No 2017-01-28 06:08
stas No 2017-01-27 22:01
trowski No 2017-01-30 04:40
weierophinney Yes 2017-02-08 17:17
yunosh Yes 2017-01-28 10:05
zeev No 2017-01-30 08:59
Allow trailing commas in use lists for anonymous functions closed
Yes
16
No
22
38 voters
adambaratz No 2017-01-28 03:54
ashnazg Yes 2017-01-27 19:18
bishop Yes 2017-02-06 17:21
bwoebi No 2017-01-27 16:18
cmb No 2017-01-29 23:41
daverandom Yes 2017-01-27 22:41
derick No 2017-01-30 09:47
dmitry No 2017-02-09 07:33
eliw No 2017-02-03 21:03
galvao No 2017-01-30 02:23
jhdxr No 2017-01-28 05:27
joey Yes 2017-02-06 22:54
kalle No 2017-01-27 16:43
kelunik Yes 2017-02-01 09:13
keyur No 2017-01-27 18:12
kguest Yes 2017-02-02 22:10
lcobucci No 2017-01-27 22:28
leigh No 2017-01-27 17:02
levim No 2017-01-27 16:26
malukenho No 2017-02-03 20:54
marcio Yes 2017-01-28 00:46
mariano Yes 2017-01-27 18:23
mbeccati No 2017-01-30 11:57
mike Yes 2017-01-31 10:02
nikic Yes 2017-01-27 16:48
ocramius Yes 2017-01-29 16:29
pajoye No 2017-01-30 03:25
pierrick No 2017-01-31 19:36
pmjones No 2017-01-31 17:16
pollita Yes 2017-02-01 01:05
salathe No 2017-02-08 19:50
sammyk Yes 2017-01-27 16:04
sebastian No 2017-01-28 06:08
stas No 2017-01-27 22:01
trowski Yes 2017-01-30 04:38
weierophinney Yes 2017-02-08 17:17
yunosh Yes 2017-01-28 10:05
zeev No 2017-01-30 09:00

Discussions on the mailing list

[RFC][DISCUSSION] Trailing commas in all list syntax
by Sammy Kaye Powers · 6 messages
Permitting trailing commas in closure use() declarations
by tyson andre · 3 messages
[RFC] Allow trailing comma in closure use list
by tyson andre · 1 message

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

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.

Trailing array 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.

Raises a parse error

<?php
use Foo\Bar\{
    Foo,
    Bar,
    Baz,
};

Proposal

This RFC proposes allowing trailing commas in all list syntax in order to:

  1. Offer a consistent API and match the existing array functionality.
  2. Make it easy to append new elements to a list in user-land.
  3. Have cleaner diffs (so only one line shows up as being modified when appending elements instead of two).
  4. It makes code reviews a tiny bit faster and for really large codebases, that time adds up. (Sara Golemon explained why HHVM added the trailing comma syntax.)

The following lists would allow trailing commas:

  1. Grouped namepaces
  2. Function/method arguments (declarations & calls)
  3. Interface implementations on a class
  4. Trait implementations on a class
  5. Class member lists
  6. Inheriting variables from the parent scope in anonymous functions

Marcio Almada posted a gist with examples of trailing commas for the various lists (shown below):

Trailing commas for all lists

<?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,
) {
	// . . . 
};

Discussion Points

There are a number of questions that have already been discussed on the internals list.

How will it work with variadics?

PHP allows for multiple splats in one call so trailing commas would work the same way.

foo(
    ...$args,
    ...$moreArgs,
    ...$evenMoreArgs,
);

Why allocate resources to make this happen

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.

You could just use leading commas

function foo(
   $bar
  ,$baz
  ,$boo
) { ... }
  1. Leading commas would break away from PSRs
  2. Parsing leading commas cause cognitive overhead whereas standard practice calls for trailing commas

The arguments for & against are weak

TL;DR:

  • For it: It's handy. (And the reasons detailed above).
  • Against: It's ugly. There are only minor gains.

Backward Incompatible Changes

This change would have no breaking changes.

Proposed PHP Version

PHP 7.2

Proposed Voting Choices

Each trailing comma list syntax has its own vote and requires a 2/3 majority to pass.

Function/method arguments (declarations & calls)

// Function/method arguments (call)
fooCall(
    $arg1,
    $arg2,
    $arg3,
);
 
// Function/method arguments (declaration)
function something(
    FooBarBazInterface $in,
    FooBarBazInterface $out,
) : bool {
}

Vote: Allow trailing commas in function/method arguments (declarations and calls) — Yes: 24 · No: 20 (closed)

Grouped namepaces

<?php
use Foo\Bar\{
    Foo,
    Bar,
    Baz,
};

Vote: Allow trailing commas in grouped namepaces — Yes: 30 · No: 9 (closed)

Interface implementations on a class

class Foo implements
    FooInterface,
    BarInterface,
    BazInterface,
{
}

Vote: Allow trailing commas in interface implementations on a class — Yes: 8 · No: 29 (closed)

Trait implementations on a class

class Foo
{
    use
        FooTrait,
        BarTrait,
        BazTrait,
    ;
}

Vote: Allow trailing commas in trait implementations on a class — Yes: 7 · No: 29 (closed)

Class member lists

class Foo
{
    const
        A = 1010,
        B = 1021,
        C = 1032,
        D = 1043,
    ;
    protected
        $a = 'foo',
        $b = 'bar',
        $c = 'baz',
    ;
    private
        $blah,
    ;
}

Vote: Allow trailing commas in class member lists — Yes: 7 · No: 29 (closed)

Inheriting variables from the parent scope in anonymous functions

$foo = function ($bar) use (
    $a,
    $b,
    $c,
) {
	// . . . 
};

Vote: Allow trailing commas in use lists for anonymous functions — Yes: 16 · No: 22 (closed)

Patches and Tests

The original patch by Sara Golemon.