iterators_006.phpt 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --TEST--
  2. ZE2 iterators and array wrapping
  3. --FILE--
  4. <?php
  5. class ai implements Iterator {
  6. private $array;
  7. function __construct() {
  8. $this->array = array('foo', 'bar', 'baz');
  9. }
  10. function rewind() {
  11. reset($this->array);
  12. $this->next();
  13. }
  14. function valid() {
  15. return $this->key !== NULL;
  16. }
  17. function key() {
  18. return $this->key;
  19. }
  20. function current() {
  21. return $this->current;
  22. }
  23. function next() {
  24. $this->key = key($this->array);
  25. $this->current = current($this->array);
  26. next($this->array);
  27. }
  28. }
  29. class a implements IteratorAggregate {
  30. public function getIterator() {
  31. return new ai();
  32. }
  33. }
  34. $array = new a();
  35. foreach ($array as $property => $value) {
  36. print "$property: $value\n";
  37. }
  38. #$array = $array->getIterator();
  39. #$array->rewind();
  40. #$array->valid();
  41. #var_dump($array->key());
  42. #var_dump($array->current());
  43. echo "===2nd===\n";
  44. $array = new ai();
  45. foreach ($array as $property => $value) {
  46. print "$property: $value\n";
  47. }
  48. echo "===3rd===\n";
  49. foreach ($array as $property => $value) {
  50. print "$property: $value\n";
  51. }
  52. ?>
  53. ===DONE===
  54. --EXPECT--
  55. 0: foo
  56. 1: bar
  57. 2: baz
  58. ===2nd===
  59. 0: foo
  60. 1: bar
  61. 2: baz
  62. ===3rd===
  63. 0: foo
  64. 1: bar
  65. 2: baz
  66. ===DONE===