rfc:list_default_value

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:list_default_value [2015/11/09 13:26] reezerfc:list_default_value [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== PHP RFC:  Default Value in List Syntax ======+====== PHP RFC:  Default Value in List Assignment Syntax ======
   * Version: 0.1   * Version: 0.1
   * Date: 2015-11-08   * Date: 2015-11-08
   * Author: Reeze Xia, reeze@php.net   * Author: Reeze Xia, reeze@php.net
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/list_default_value   * First Published at: http://wiki.php.net/rfc/list_default_value
  
Line 9: Line 9:
 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.  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 purpose 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.+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. 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.
Line 15: Line 15:
 ===== Proposal ===== ===== Proposal =====
  
-Support new syntax to set default value for list elements, when there the requested index it not found in array:+Support new syntax to set default values for list elements, when the requested index it is not found in array:
  
 <code php> <code php>
 +// basic syntax
 list($a, $b='default') = [1];      // a = 1, b = 'default' list($a, $b='default') = [1];      // a = 1, b = 'default'
 list($a, $b='default') = [1, 2];  // a = 1, b = 2 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() function say_hello()
 { {
Line 37: Line 51:
 <code php> <code php>
  
-list($a, $b='deafult') = $arr;+list($a, $b='default') = $arr;
  
 // equals // equals
Line 81: Line 95:
  
 ===== RFC Impact ===== ===== RFC Impact =====
-==== To SAPIs ==== 
-No impact 
- 
-==== To Existing Extensions ==== 
-No impact 
  
 ==== To Opcache ==== ==== To Opcache ====
  
 I am working on opcache compatibility. I am working on opcache compatibility.
- 
-==== New Constants ==== 
-None 
- 
-==== php.ini Defaults ==== 
-None 
  
 ===== Open Issues ===== ===== Open Issues =====
 None for now None for now
- 
-===== Unaffected PHP Functionality ===== 
-No BC break 
  
 ===== Future Scope ===== ===== Future Scope =====
- 
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
rfc/list_default_value.1447075575.txt.gz · Last modified: 2017/09/22 13:28 (external edit)