From: shooric_ at mail dot ru
Operating system: Linux3.2.0-49
PHP version: 5.5.0
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Unexpected behavior of the "yield"
Description:
------------
When you interrupt iterations on "yield" generator via "break" instruction,
generator function does not work out until the end of.
Test script:
---------------
<?php
$filename = '111111.txt';
$h = fopen($filename, 'w+');
for($i = 0; $i < 7; $i++) {
echo 'write: ' . $i . PHP_EOL;
fwrite($h, $i . PHP_EOL);
}
fclose($h);
function file_line_iterator($filename) {
echo 'open file' . PHP_EOL;
$h = fopen($filename, 'r');
while(false !== $line = fgets($h)) {
echo 'yield: ' . $line;
yield $line;
}
echo 'close file' . PHP_EOL;
fclose($h);
}
$k = 0;
foreach(file_line_iterator($filename) as $k => $v) {
echo 'iterate: ' . $v;
if(++$k > 4) {
break;
}
}
echo 'exit' . PHP_EOL;
exit;
Expected result:
----------------
write: 0
write: 1
write: 2
write: 3
write: 4
write: 5
write: 6
open file
yield: 0
iterate: 0
yield: 1
iterate: 1
yield: 2
iterate: 2
yield: 3
iterate: 3
yield: 4
iterate: 4
* close file
exit
Actual result:
--------------
write: 0
write: 1
write: 2
write: 3
write: 4
write: 5
write: 6
open file
yield: 0
iterate: 0
yield: 1
iterate: 1
yield: 2
iterate: 2
yield: 3
iterate: 3
yield: 4
iterate: 4
exit
--
Edit bug report at https://bugs.php.net/bug.php?id=65235&edit=1
--
Try a snapshot (PHP 5.4):
https://bugs.php.net/fix.php?id=65235&r=trysnapshot54
Try a snapshot (PHP 5.3):
https://bugs.php.net/fix.php?id=65235&r=trysnapshot53
Try a snapshot (trunk):
https://bugs.php.net/fix.php?id=65235&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=65235&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=65235&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=65235&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=65235&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=65235&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=65235&r=support
Expected behavior: https://bugs.php.net/fix.php?id=65235&r=notwrong
Not enough info:
https://bugs.php.net/fix.php?id=65235&r=notenoughinfo
Submitted twice:
https://bugs.php.net/fix.php?id=65235&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=65235&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65235&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=65235&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=65235&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=65235&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65235&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=65235&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=65235&r=mysqlcfg