generator_method.phpt 477 B

1234567891011121314151617181920212223242526272829
  1. --TEST--
  2. Methods can be generators
  3. --FILE--
  4. <?php
  5. class Test implements IteratorAggregate {
  6. protected $data;
  7. public function __construct(array $data) {
  8. $this->data = $data;
  9. }
  10. public function getIterator() {
  11. foreach ($this->data as $value) {
  12. yield $value;
  13. }
  14. }
  15. }
  16. $test = new Test(['foo', 'bar', 'baz']);
  17. foreach ($test as $value) {
  18. var_dump($value);
  19. }
  20. ?>
  21. --EXPECT--
  22. string(3) "foo"
  23. string(3) "bar"
  24. string(3) "baz"