rfc:trailing-comma-function-args

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,
    );
}

Vote

Note: Consideration for 5.6 inclusion, not 5.5.

Should the current optional trailing comma implementation be merged
Real name Yes No
aharvey (aharvey)  
andi (andi)  
auroraeosrose (auroraeosrose)  
brianlmoon (brianlmoon)  
danbeck (danbeck)  
derick (derick)  
dm (dm)  
dmitry (dmitry)  
fa (fa)  
gwynne (gwynne)  
hradtke (hradtke)  
indeyets (indeyets)  
ircmaxell (ircmaxell)  
kalle (kalle)  
lbarnaud (lbarnaud)  
levim (levim)  
lstrojny (lstrojny)  
marco (marco)  
mfonda (mfonda)  
mgf (mgf)  
mike (mike)  
nikic (nikic)  
patrickallaert (patrickallaert)  
pierrick (pierrick)  
pollita (pollita)  
ramsey (ramsey)  
reeze (reeze)  
salathe (salathe)  
sascham78 (sascham78)  
stas (stas)  
treffynnon (treffynnon)  
tyrael (tyrael)  
uw (uw)  
wez (wez)  
zeev (zeev)  
Final result: 15 20
This poll has been closed.

Changelog

  • Expanded proposal to include declarations as well as arguments 2013-02-20 11:23 GMT
rfc/trailing-comma-function-args.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1