rfc:php7_foreach

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:php7_foreach [2015/01/29 19:26] dmitryrfc: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: Draft+  * Status: Implemented (in PHP 7.0)
   * First Published at: http://wiki.php.net/rfc/php7_foreach   * First Published at: http://wiki.php.net/rfc/php7_foreach
  
Line 74: Line 74:
 3 3
 </code> </code>
- 
-==== foreach() by value over plain objects ==== 
- 
-TODO 
  
 ==== foreach() by reference over arrays ==== ==== foreach() by reference over arrays ====
Line 159: Line 155:
 </code> </code>
  
-Modification of array, iterated through foreach by reference, using internal functions like array_pop(), array_push(), array_shift(), array_unshift() works consistently. (**It didn't work in PHP5**)+Modification of array, iterated through foreach by reference, using internal functions like array_pop(), array_push(), array_shift(), array_unshift() works consistently. These functions preserve the current foreach position or move it to the following element, if the current is deleted. (**It didn't work in PHP5**)
  
 <code> <code>
Line 166: Line 162:
 2 2
 </code> </code>
 +
 +==== 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/FE_FETCH opcodes are split into separate FE_RESET_R/FE_FETCH_R opcodes used to implement **foreach by value** and FE_RESET_RW/FE_FETCH_RW to implement **foreach by reference**. The suffix **_R** means that we use array (or object) only for reading, and suffix **_RW** that we also may indirectly modify it. A new FE_FREE opcode is introduced. It's used at the end of **foreach by reference** loops, instead of FREE opcode.+The existing FE_RESET/FE_FETCH opcodes are split into separate FE_RESET_R/FE_FETCH_R opcodes used to implement **foreach by value** and FE_RESET_RW/FE_FETCH_RW to implement **foreach by reference**. The suffix **_R** means that we use array (or object) only for reading, and suffix **_RW** that we also may indirectly modify it. A new FE_FREE opcode is introduced. It's used at the end of **foreach** loops, instead of FREE opcode.
  
-Iteration by value don't use or modify internal array (or object) 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 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 or by value over plain object implemented using special **HashTableIterator** structures.
  
 <code> <code>
Line 186: Line 186:
 </code> </code>
  
-On entrance into **foreach by reference** FE_RESET_RW opcode creates and initializes a new iterator, increments the counter of such iterators for given HachTable and store its index in reserved space of temporary variable used for iteration. On exit, FE_FREE opcode removes corresponding iterator and decrements the iterators counter of corresponding hash table.+On entrance into **foreach** loop FE_RESET_R/RW opcode creates and initializes a new iterator and stores its index in reserved space of temporary variable used for iteration. On exit, FE_FREE opcode removes corresponding iterator.
  
 Iterators are actually allocated in a buffer - EG(ht_iterators), represented by plain array. The more nested **foreach by reference** iterators the bigger buffer we will need. We start with small preallocated buffer - EG(ht_iterators_slots), and then extend it if necessary in heap. EG(ht_iterators_count) keeps the number of available slots for iterators, EG(ht_iterators_used) - the number of used slots. Iterators are actually allocated in a buffer - EG(ht_iterators), represented by plain array. The more nested **foreach by reference** iterators the bigger buffer we will need. We start with small preallocated buffer - EG(ht_iterators_slots), and then extend it if necessary in heap. EG(ht_iterators_count) keeps the number of available slots for iterators, EG(ht_iterators_used) - the number of used slots.
Line 212: Line 212:
  
 <code> <code>
-ZEND_API void         zend_hash_iterators_update(HashTable *ht, HashPosition pos);+ZEND_API void         zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to);
 </code> </code>
  
Line 231: Line 231:
   * tests/lang/foreachLoopObjects.006.phpt - replacement of array in foreach by value doesn't have effect   * tests/lang/foreachLoopObjects.006.phpt - replacement of array in foreach by value doesn't have effect
  
 +===== Additional Behavoir Change =====
  
 +With new implementation it's quite easy to stop using internal array/object pointer even for *foreach be referece*.
 +It means that reset/key/current/next/prev function will be completely independent from the sate of *foreach* iterator.
 +This would change the output of few examples above.
  
 +**foreach** (even foreach by  reference) won't affect internal array pointer
  
 +<code>
 +$ php -r '$a = [1,2,3]; foreach($a as &$v) {echo $v . " - " . current($a) . "\n"; }'
 +1 - 1
 +2 - 1
 +3 - 1
 +</code>
  
 +Modification of internal array pointer through next() and family doesn't affect **foreach** pointer. But it also won't be affected by the value of **forech** pointer.
 +
 +<code>
 +$ 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)
 +</code> 
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 248: Line 268:
  
 ===== Open Issues ===== ===== Open Issues =====
-  * complete RFC +  * implementation optimization for size & speed
-  * complete implementation +
-  * performance optimization +
  
 ===== 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="Fix foreach behavoir?" auth="dmitry" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
 +
 +The second (Yes/No 50%+1) question is - if we should stop modifying internal array/object pointer in **foreach**.
 +
 +<doodle title="Stop using internal array/object pointer in foreach by reference?" auth="dmitry" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
 +
 +The vote will end on February 12.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 262: Line 294:
 Pull request for master branch: [[https://github.com/php/php-src/pull/1034]] Pull request for master branch: [[https://github.com/php/php-src/pull/1034]]
  
-It doesn't solve all the problems yet.+The implementation of additional idea is trivial [[https://gist.github.com/dstogov/63b269207ba0aed8b776]]
  
 ===== Implementation ===== ===== Implementation =====
-After the project is implemented, this section should contain  +The RFC implemented in PHP7 with two commits: 
-  - the version(s) it was merged to + 
-  a link to the git commit(s) +[[http://git.php.net/?p=php-src.git;a=commitdiff;h=97fe15db4356f8fa1b3b8eb9bb1baa8141376077|97fe15db4356f8fa1b3b8eb9bb1baa8141376077]]
-  - link to the PHP manual entry for the feature+
  
-===== References ===== +[[http://git.php.net/?p=php-src.git;a=commitdiff;h=4d2a575db2ac28c9acede4a85152bcec342c4a1d|4d2a575db2ac28c9acede4a85152bcec342c4a1d]]
-Links to external references, discussions or RFCs+
  
  
rfc/php7_foreach.1422559593.txt.gz · Last modified: 2017/09/22 13:28 (external edit)