iterators_007.phpt 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. --TEST--
  2. ZE2 iterators and exceptions
  3. --FILE--
  4. <?php
  5. class Test implements Iterator
  6. {
  7. public $arr = array(1, 2, 3);
  8. public $x = 0;
  9. public function rewind(): void { if ($this->x == 0) throw new Exception(__METHOD__); reset($this->arr); }
  10. public function current(): mixed { if ($this->x == 1) throw new Exception(__METHOD__); return current($this->arr); }
  11. public function key(): mixed { if ($this->x == 2) throw new Exception(__METHOD__); return key($this->arr); }
  12. public function next(): void { if ($this->x == 3) throw new Exception(__METHOD__); next($this->arr); }
  13. public function valid(): bool { if ($this->x == 4) throw new Exception(__METHOD__); return (key($this->arr) !== NULL); }
  14. }
  15. $t = new Test();
  16. while($t->x < 5)
  17. {
  18. try
  19. {
  20. foreach($t as $k => $v)
  21. {
  22. echo "Current\n";
  23. }
  24. }
  25. catch(Exception $e)
  26. {
  27. echo "Caught in " . $e->getMessage() . "()\n";
  28. }
  29. $t->x++;
  30. }
  31. ?>
  32. --EXPECT--
  33. Caught in Test::rewind()
  34. Caught in Test::current()
  35. Caught in Test::key()
  36. Current
  37. Caught in Test::next()
  38. Caught in Test::valid()