rfc:list_default_value
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:list_default_value [2015/11/09 12:18] – reeze | rfc:list_default_value [2015/11/10 15:33] – Typo fixes reeze | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Default Value in List Syntax ====== | + | ====== PHP RFC: Default Value in List Assignment |
* 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: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | We could destruct | + | We could destruct |
- | For the similar | + | For the similar |
- | One could check the array himself | + | Some clever users figured out some workaround like this [[http:// |
===== Proposal ===== | ===== Proposal ===== | ||
- | All the features and examples of the proposal. | ||
- | To [[http:// | + | Support new syntax to set default values for list elements, when the requested index it is not found in array: |
- | for inclusion | + | |
- | Remember that the RFC contents should be easily reusable in the PHP Documentation. | + | <code php> |
+ | // basic syntax | ||
+ | list($a, $b=' | ||
+ | list($a, $b=' | ||
- | ===== Backward Incompatible Changes ===== | + | // comparation |
- | What breaks, and what is the justification for it? | + | list($a, list($b=1, $c=2)) = $arr; |
+ | // or we need to check it ourself | ||
+ | if (!isset($arr[1][0])) { | ||
+ | $arr[1][0] | ||
+ | } | ||
+ | if (!isset($arr[1][1])) { | ||
+ | $arr[1][0] | ||
+ | } | ||
- | ===== Proposed PHP Version(s) ===== | + | list($a, list($b, $c)) = $arr; |
- | List the proposed PHP versions that the feature will be included in. Use relative versions such as "next PHP 5.x" or "next PHP 5.x.y" | + | |
- | ===== RFC Impact ===== | ||
- | ==== To SAPIs ==== | ||
- | Describe the impact to CLI, Development web server, embedded PHP etc. | ||
- | ==== To Existing Extensions | + | // other examples |
- | Will existing extensions be affected? | + | function say_hello() |
+ | { | ||
+ | return " | ||
+ | } | ||
+ | $name = ' | ||
+ | list($a=say_hello(), | ||
- | ==== To Opcache | + | list($a, list($b=1, $c=2)) = [1]; // a = 1, b = 1, c = 2 |
- | It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP. | + | |
- | Please explain how you have verified your RFC's compatibility with opcache. | + | </ |
- | ==== New Constants ==== | + | The assignment could be considered as a shortcut for: |
- | Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation. | + | |
- | ==== php.ini Defaults ==== | + | < |
- | If there are any php.ini settings then list: | + | |
- | * hardcoded default values | + | |
- | * php.ini-development values | + | |
- | * php.ini-production values | + | |
- | ===== Open Issues ===== | + | list($a, $b=' |
- | Make sure there are no open issues when the vote starts! | + | |
- | ===== Unaffected PHP Functionality ===== | + | // equals |
- | List existing areas/features of PHP that will not be changed by the RFC. | + | |
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC' | + | $a = $arr[0]; |
+ | $b = isset($arr[1]) ? $arr[1] : ' | ||
+ | </ | ||
+ | |||
+ | 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: | ||
+ | |||
+ | <code flex> | ||
+ | assignment_list_element: | ||
+ | variable | ||
+ | + | variable ' | ||
+ | | T_LIST ' | ||
+ | | /* empty */ | ||
+ | </ | ||
+ | |||
+ | The syntax is same as function' | ||
+ | |||
+ | |||
+ | We could also set default value in foreach list context: | ||
+ | |||
+ | <code php> | ||
+ | foreach($arr as list($k, list($v1=' | ||
+ | // do something | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | More case could be found from PR's tests: | ||
+ | |||
+ | This feature could also be found in [[https:// | ||
+ | |||
+ | ===== 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 ===== | ===== Future Scope ===== | ||
- | This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC. | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Include these so readers know where you are heading and can discuss the proposed voting options. | ||
- | State whether this project requires a 2/3 or 50%+1 majority (see [[voting]]) | + | * Whether accept the RFC for PHP 7.1 |
+ | |||
+ | This project requires a 2/3 majority (see [[voting]]) | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Links to any external patches and tests go here. | ||
- | If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. | + | * Patch: [[https:// |
- | Make it clear if the patch is intended to be the final patch, or is just a prototype. | + | I am working on opcache compatibility |
===== Implementation ===== | ===== Implementation ===== | ||
Line 78: | Line 124: | ||
===== References ===== | ===== References ===== | ||
- | Links to external references, discussions or RFCs | + | |
+ | * Javascript destructing: | ||
+ | * Clojure destructing [[https:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | Keep this updated with features that were discussed on the mail lists. | + | |
+ | ===== Changelog ===== | ||
+ | |||
+ | * v0.1 - Initial version |
rfc/list_default_value.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1