iterators_007.phpt 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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() { if ($this->x == 0) throw new Exception(__METHOD__); reset($this->arr); }
  10. public function current() { if ($this->x == 1) throw new Exception(__METHOD__); return current($this->arr); }
  11. public function key() { if ($this->x == 2) throw new Exception(__METHOD__); return key($this->arr); }
  12. public function next() { if ($this->x == 3) throw new Exception(__METHOD__); next($this->arr); }
  13. public function valid() { 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. ===DONE===
  33. --EXPECT--
  34. Caught in Test::rewind()
  35. Caught in Test::current()
  36. Caught in Test::key()
  37. Current
  38. Caught in Test::next()
  39. Caught in Test::valid()
  40. ===DONE===