|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-09-22 12:07 UTC] cmb@php.net
-Summary: yield from ignores yield-by-reference
+Summary: Yield from by reference is not supported
-Type: Bug
+Type: Documentation Problem
[2021-09-22 12:07 UTC] cmb@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 12:00:01 2025 UTC |
Description: ------------ When "yield from" was implemented, we did not consider how it interacts with yield-by-reference. Right now it is simply ignored and "yield from" yields all values by-value. Likely this is not the expected or intended behavior. Instead "yield from" in a by-reference generator should yield array values and values from by-reference generators by-reference. "yield from" should generate an error for iterators (and generators) that do not support by-reference iteration. Test script: --------------- <?php function &gen(array $array) { yield from $array; } $array = [1, 2, 3]; foreach (gen($array) as &$v) { $v += 1; } var_dump($array); Expected result: ---------------- array(3) { [0]=> int(2) [1]=> int(3) [2]=> int(4) } Actual result: -------------- array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }