rfc:foreachlist
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
rfc:foreachlist [2011/12/19 01:20] levim [Introduction] grammar and spelling improvements. |
rfc:foreachlist [2017/09/22 13:28] (current) |
||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2011-07-06 | * Date: 2011-07-06 | ||
* Author: Xinchen Hui < | * Author: Xinchen Hui < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | T_LIST cannot be used with foreach before. | + | This feature introduces |
- | <code php> | + | |
- | <?php | + | |
- | foreach (array(array(1, | + | |
- | /* won't work */ | + | |
- | } | + | |
- | </ | + | |
- | It is also a feature request: | ||
- | |||
- | I think this feature could make PHP code more flexible, so I am working on a solution to provide that. | ||
- | |||
- | ===== Proposal ===== | ||
- | 1. ZEND_FE_FETCH | ||
<code php> | <code php> | ||
<?php | <?php | ||
- | foreach (array(array(24,2333), array(31,4666)) as list($a,$b)) { | + | $users = array( |
- | | + | |
- | } | + | |
- | /** output: | + | ); |
- | a=>24, b=> | + | |
- | a=>31, b=> | + | |
- | */ | + | |
- | </ | + | |
- | 2. ZEND_FE_FETCH_WITH_KEY | + | // Before |
- | <code php> | + | foreach ($users |
- | <?php | + | |
- | foreach (array(array(24, | + | |
- | | + | |
} | } | ||
- | /** output: | ||
- | key:0, a=>24, b=>2333 | ||
- | key:1, a=>31, b=>4666 | ||
- | */ | ||
- | </ | ||
- | Opcodes generated: | + | // After |
- | + | foreach ($users | |
- | list.php | + | echo "First name: $firstName, last name: $lastName. "; |
- | <code php> | + | |
- | <?php | + | |
- | foreach (array(array(1, | + | |
- | echo $a, $b; | + | |
} | } | ||
</ | </ | ||
- | < | + | This feature eliminates the use of a redundant variable ($user in the example above), reduces code verbosity in typical cases of iterating structured data, such as SQL result sets, and it doesn' |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 10 ASSIGN | + | |
- | 11 FETCH_DIM_R | + | |
- | 12 ASSIGN | + | |
- | | + | |
- | 14 ECHO !1 | + | |
- | | + | |
- | 16 > | + | |
- | </ | + | |
- | list_with_key.php | + | It's a commonly requested feature, and there is evidence that people already expect list() should work in this scenario: |
- | < | + | |
- | <?php | + | |
- | $array | + | |
- | | + | |
- | array(3, 4) | + | |
- | ); | + | |
- | foreach ($array as $k => list($a, $b)) { | + | This RFC provides |
- | echo $k, $a, $b; | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 10 FETCH_DIM_R | + | |
- | 11 ASSIGN | + | |
- | 12 FETCH_DIM_R | + | |
- | 13 ASSIGN | + | |
- | 14 ASSIGN | + | |
- | | + | |
- | 16 ECHO !2 | + | |
- | 17 ECHO !3 | + | |
- | | + | |
- | 19 > | + | |
- | 10 20 > RETURN | + | |
- | </ | + | |
- | ===== More about RFC ===== | + | ===== Implementation details of the RFC ===== |
- | In order to avoid the reduce/ | + | In order to avoid the reduce/ |
- | considering this script: | ||
<code php> | <code php> | ||
<?php | <?php | ||
Line 123: | Line 45: | ||
</ | </ | ||
- | before patched: | + | Without the new patch: |
< | < | ||
PHP Parse error: | PHP Parse error: | ||
</ | </ | ||
- | after patched: | + | With the new patch: |
< | < | ||
PHP Fatal error: | PHP Fatal error: | ||
Line 135: | Line 57: | ||
</ | </ | ||
- | ===== Silent Token ====== | + | ===== Possible additional features (vote separately): |
- | about silent token ' | + | It is possible |
<code php> | <code php> | ||
- | foreach (array(array(1, | + | $array = array(array(1, |
- | } | + | foreach ($array |
+ | } 1 | ||
</ | </ | ||
- | if yes, the second patch in the following patchs list can be used. | ||
- | Opcodes generated: | + | ===== Patches ===== |
+ | * foreach list: https:// | ||
+ | * foreach list with supporting of silent token: https:// | ||
+ | ===== Vote ===== | ||
+ | <doodle | ||
+ | title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
- | list_with_silent.php | + | <doodle |
- | <code php> | + | title=" |
- | <?php | + | |
- | foreach | + | |
- | echo $a, $b; /* without the silent token, there will be a notice since the third item of array has no index 1*/ | + | </doodle> |
- | } | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 10 > | + | |
- | 11 BEGIN_SILENCE | + | |
- | 12 FETCH_DIM_R | + | |
- | 13 FETCH_W | + | |
- | 14 ASSIGN | + | |
- | 15 FETCH_DIM_R | + | |
- | 16 FETCH_W | + | |
- | 17 ASSIGN | + | |
- | 18 END_SILENCE | + | |
- | | + | |
- | 20 ECHO !1 | + | |
- | | + | |
- | 22 > | + | |
- | | + | |
- | </ | + | |
- | ===== Patchs ===== | + | |
- | | + | |
- | | + | |
- | ===== Tests ===== | + | |
- | * http:// | + | |
- | * http:// | + | |
===== Changelog ===== | ===== Changelog ===== | ||
* 2011-07-06 Xinchen Hui: Initial RFC creation | * 2011-07-06 Xinchen Hui: Initial RFC creation | ||
Line 193: | Line 88: | ||
* 2011-07-08 Xinchen Hui: Added opcodes with silent token | * 2011-07-08 Xinchen Hui: Added opcodes with silent token | ||
* 2011-07-24 Xinchen Hui: Updated patches that fixed a bug: znode-> | * 2011-07-24 Xinchen Hui: Updated patches that fixed a bug: znode-> | ||
+ | * 2012-07-18 Phidev: Rewrote the RFC | ||
+ | * 2012-08-18 Xinchen Hui: Open voting | ||
+ | * 2012-08-25 Xinchen Hui: Close voting | ||
+ |
rfc/foreachlist.1324257655.txt.gz · Last modified: 2017/09/22 13:28 (external edit)