====== PHP RFC: Default Value in List Assignment Syntax ======
* Version: 0.1
* Date: 2015-11-08
* Author: Reeze Xia, reeze@php.net
* Status: Under Discussion
* First Published at: http://wiki.php.net/rfc/list_default_value
===== Introduction =====
We could destruct variables from an array with [[http://php.net/list| list constuct]], it may be nested or a simple array. But there is no guarantee that the array can fulfill all variables, it will be simple assigned with null with notice error (not good). We will need several redundant code to handle the possible cases if we want to check it and assign it with default values.
For the similar reason we introduced [[http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison|Null coalesce "??"]] and [[http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary|Ternary Operator "?:"]] to help write clean code.
Some clever users figured out some workaround like this [[http://php.net/manual/en/function.list.php#113189|User contributed notes]]. But that is not good enough and ugly.
===== Proposal =====
Support new syntax to set default values for list elements, when the requested index it is not found in array:
// basic syntax
list($a, $b='default') = [1]; // a = 1, b = 'default'
list($a, $b='default') = [1, 2]; // a = 1, b = 2
// comparation
list($a, list($b=1, $c=2)) = $arr;
// or we need to check it ourself
if (!isset($arr[1][0])) {
$arr[1][0] = 1;
}
if (!isset($arr[1][1])) {
$arr[1][0] = 2;
}
list($a, list($b, $c)) = $arr;
// other examples
function say_hello()
{
return "Hello";
}
$name = 'PHP';
list($a=say_hello(), $b=$name."7.0") = []; // a = 'Hello', b = 'PHP7.0'
list($a, list($b=1, $c=2)) = [1]; // a = 1, b = 1, c = 2
The assignment could be considered as a shortcut for:
list($a, $b='default') = $arr;
// equals
$a = $arr[0];
$b = isset($arr[1]) ? $arr[1] : 'default';
This seems not a lot of work than the new syntax. But sometimes we need to destruct nested array and there are morn than two variables, anyway list() itself is designed to avoid assign by hand for short and clean code.
The syntax is:
assignment_list_element:
variable
+ | variable '=' expr # the new syntax. value could be any expression
| T_LIST '(' assignment_list ')'
| /* empty */
The syntax is same as function's default value for parameters.
We could also set default value in foreach list context:
foreach($arr as list($k, list($v1='deafult', $v2='default2'))) {
// do something
}
More case could be found from PR's tests: [[https://github.com/php/php-src/pull/1623/files]]
This feature could also be found in [[https://clojurebridge.github.io/community-docs/docs/clojure/destructuring/|Clojure]] and [[https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/|Javascript]] for destructing.
===== Backward Incompatible Changes =====
No BC break.
===== Proposed PHP Version(s) =====
PHP 7.1
===== RFC Impact =====
==== To Opcache ====
I am working on opcache compatibility.
===== Open Issues =====
None for now
===== Future Scope =====
===== Proposed Voting Choices =====
* Whether accept the RFC for PHP 7.1
This project requires a 2/3 majority (see [[voting]])
===== Patches and Tests =====
* Patch: [[https://github.com/php/php-src/pull/1623]]
I am working on opcache compatibility
===== Implementation =====
After the project is implemented, this section should contain
- the version(s) it was merged to
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
===== References =====
* Javascript destructing: [[https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/]]
* Clojure destructing [[https://clojurebridge.github.io/community-docs/docs/clojure/destructuring/]]
===== Rejected Features =====
===== Changelog =====
* v0.1 - Initial version