This is an old revision of the document!
PHP RFC: Allow trailing comma in closure use list
- Date: 2020-07-01
- Author: Tyson Andre tandre@php.net
- Status: Draft
- Target Version: PHP 8.0
- Implementation: https://github.com/php/php-src/pull/5306
Introduction
This RFC proposes to allow an optional trailing comma in closure use lists. This is already supported for argument and parameter lists.
Consider this example:
$longArgs_longVars = function ( $longArgument, $longerArgument, $muchLongerArgument, ) use ( $longVar1, $longerVar2, $muchLongerVar3 ) { // body };
This constructor has too many parameters and closure use variables to place all of them on one line, at least under conventional coding standards. In such cases PSR-12 recommends to use the formatting above instead (PSR-12 predates support for trailing commas in parameter lists).
Unfortunately, it is currently not possible to place a trailing comma in the closure use list. This breaks uniformity, and results in larger diffs when a new optional closure use variable is added. Additionally, it is inconsistent with parameter lists and call-sites, which do allow a trailing comma.
At this point, I'm used to always adding a trailing comma to multi-line lists, regardless of what kind of element they contain. We should avoid having this kind of arbitrary restriction.
Proposal
Allow a single optional trailing comma in closure use lists.
Prior Art
Allow trailing comma in parameter list added support for trailing commas in parameter lists.
Allow a trailing comma in function calls added support for trailing commas in calls.
Trailing Commas In List Syntax was a previous proposal to support optional trailing commas in all list-like structures.
Nothing has substantially changed since those RFCs in technical terms, but I think the aforementioned style for breaking up large lists has become more accepted in the meantime.
Backward Incompatible Changes
None.
Vote
Yes/No, requiring 2/3 majority