rfc:php7_foreach
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:php7_foreach [2015/01/29 19:26] – dmitry | rfc:php7_foreach [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2015-01-29 | * Date: 2015-01-29 | ||
* Author: Dmitry Stogov, dmitry@zend.com | * Author: Dmitry Stogov, dmitry@zend.com | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 74: | Line 74: | ||
3 | 3 | ||
</ | </ | ||
- | |||
- | ==== foreach() by value over plain objects ==== | ||
- | |||
- | TODO | ||
==== foreach() by reference over arrays ==== | ==== foreach() by reference over arrays ==== | ||
Line 159: | Line 155: | ||
</ | </ | ||
- | Modification of array, iterated through foreach by reference, using internal functions like array_pop(), | + | Modification of array, iterated through foreach by reference, using internal functions like array_pop(), |
< | < | ||
Line 166: | Line 162: | ||
2 | 2 | ||
</ | </ | ||
+ | |||
+ | ==== foreach() by value over plain objects ==== | ||
+ | |||
+ | It beahves in the same way as **foreach by reference over array**, but using object value instead of reference. As result the object can be modified, but can't be replaced. | ||
==== foreach() by reference over plain objects ==== | ==== foreach() by reference over plain objects ==== | ||
Line 173: | Line 173: | ||
==== Implementation Details ==== | ==== Implementation Details ==== | ||
- | The existing FE_RESET/ | + | The existing FE_RESET/ |
- | Iteration by value don't use or modify internal array (or object) | + | Iteration by value over array doesn't use or modify internal array pointer. The value of the pointer is kept in reserved space of temporary variable used for iteration. It's acceptable through Z_FE_POS() macro. |
- | Iteration by reference implemented using special **HashTableIterator** structures. | + | Iteration by reference |
< | < | ||
Line 186: | Line 186: | ||
</ | </ | ||
- | On entrance into **foreach | + | On entrance into **foreach** |
Iterators are actually allocated in a buffer - EG(ht_iterators), | Iterators are actually allocated in a buffer - EG(ht_iterators), | ||
Line 212: | Line 212: | ||
< | < | ||
- | ZEND_API void | + | ZEND_API void |
</ | </ | ||
Line 231: | Line 231: | ||
* tests/ | * tests/ | ||
+ | ===== Additional Behavoir Change ===== | ||
+ | With new implementation it's quite easy to stop using internal array/ | ||
+ | It means that reset/ | ||
+ | This would change the output of few examples above. | ||
+ | **foreach** (even foreach by reference) won't affect internal array pointer | ||
+ | < | ||
+ | $ php -r '$a = [1,2,3]; foreach($a as &$v) {echo $v . " - " . current($a) . " | ||
+ | 1 - 1 | ||
+ | 2 - 1 | ||
+ | 3 - 1 | ||
+ | </ | ||
+ | Modification of internal array pointer through next() and family doesn' | ||
+ | |||
+ | < | ||
+ | $ php -r '$a = [1,2,3,4]; foreach($a as &$v) {echo "$v - "; next($a); var_dump(current($a)); | ||
+ | 1 - int(2) | ||
+ | 2 - int(3) | ||
+ | 3 - int(4) | ||
+ | 4 - bool(false) | ||
+ | </ | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 248: | Line 268: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | * complete RFC | + | * implementation optimization |
- | * complete | + | |
- | * performance | + | |
===== Future Scope ===== | ===== Future Scope ===== | ||
Line 257: | Line 275: | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
The vote is a straight Yes/No vote, that requires a 2/3 majority | The vote is a straight Yes/No vote, that requires a 2/3 majority | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | The second (Yes/No 50%+1) question is - if we should stop modifying internal array/ | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | The vote will end on February 12. | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 262: | Line 294: | ||
Pull request for master branch: [[https:// | Pull request for master branch: [[https:// | ||
- | It doesn' | + | The implementation of additional idea is trivial [[https:// |
===== Implementation ===== | ===== Implementation ===== | ||
- | After the project is implemented, this section should contain | + | The RFC implemented |
- | - the version(s) it was merged to | + | |
- | - a link to the git commit(s) | + | [[http:// |
- | - a link to the PHP manual entry for the feature | + | |
- | ===== References ===== | + | [[http:// |
- | Links to external references, discussions or RFCs | + | |
rfc/php7_foreach.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1