|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77515 A generator is closed by making a call to Generator::valid
Submitted: 2019-01-24 12:57 UTC Modified: -
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: knyjoh at gmail dot com Assigned:
Status: Open Package: *General Issues
PHP Version: 7.3.1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: knyjoh at gmail dot com
New email:
PHP Version: OS:


 [2019-01-24 12:57 UTC] knyjoh at gmail dot com
A generator is closed by making a call to Generator::valid

It is not the expected behavior that a call to valid() will actually change a state. If it is considered a correct behavior it should be documented.
Note that the same problem exists with the current() function.

In example 1 it is clear that when reaching the loop the generator is not closed, because the loop does not fail. Traversing a closed generator is not allowed and would throw an exception, which means that the generator must be open at this stage.

In example 2 however, when reaching the loop an exception is thrown "Cannot traverse an already closed generator". The generator has therefore been closed by the call to valid().

Test script:
1. <?php
3. function generator() {
4.   if (false) {
5.     yield 1; // Force generator
6.   }
7. }
9. // Example 1
10. $values = generator();
11. foreach ($values as $value) {} // This is ok
13. // Example 2
14. $values = generator();
15. $values->valid();
16. foreach ($values as $value) {} // This is not ok

Actual result:
Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /in/ab0KK:16
Stack trace:
#0 {main}
  thrown in /in/ab0KK on line 16


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 16:01:26 2019 UTC