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/30 07:05] 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: Under Discussion+  * 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 155: 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 230: Line 230:
   * tests/lang/foreachLoop.015.phpt - modification of array in foreach by reference through internal functions   * tests/lang/foreachLoop.015.phpt - modification of array in foreach by reference through internal functions
   * 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 250: 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 255: 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.1422601551.txt.gz · Last modified: 2017/09/22 13:28 (external edit)