pollita

Sara Golemon

Contents

Request for Comments: Trailing comma function args

Introduction

PHP has long supported trailing commas in array declarations like so:

<?php
$a = array(
       'Orange',
       'Apple',
       'Banana',
      );

This is useful for VCS (cvs, svn, git, hg, etc…) since individual lines in the list may be modified, added, or removed without having to touch unrelated lines which happen to be at the end of the list.

Function call arguments do not share this trait.

<?php
// This is an error
$fp = fopen(
        "sample.txt",
        "r+",
       );

Which means that adding additional parameters to the call forces touching both the new line and the prior one, which is bad for VCS history.

Similarly, function declarations have the same inconsistency with array() and list().

<?php
// Invalid currently
function foo(
           $bar,
           ) {
  /* ... */
}

This RFC proposes to allow trailing commas in function and method call argument lists and function argument declarations.

The patch

What's left out on purpose

Bonus comma in the middle of a void argument list

  • It implies two ignored args, rather than one
  • It doesn't actually help the VCS blame issue anyway
<?php
phpinfo(
    ,
    );

Trailing comma in yield() expression

  • yield only takes one value (or key⇒val expression)
  • yield is not really a function call, even if it looks like one
<?php
function mygen() {
  yield(
    123,
    );
}

Versions

Version Changed Date
1 Expanded proposal to include declarations as well as arguments 11:23 GMT

Votes

An option needs 50%+1 votes to win

Should the current optional trailing comma implementation be merged (42.9% approved)
User Vote
aharvey Yes
andi No
auroraeosrose Yes
brianlmoon No
danbeck No
derick No
dm No
dmitry No
fa No
gwynne No
hradtke No
indeyets Yes
ircmaxell Yes
kalle No
lbarnaud Yes
levim No
lstrojny No
marco Yes
mfonda Yes
mgf No
mike Yes
nikic No
patrickallaert Yes
pierrick Yes
pollita Yes
ramsey Yes
reeze No
salathe No
sascham78 No
stas No
treffynnon Yes
tyrael Yes
uw No
wez Yes
zeev No