foreachLoopIterator.001.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. --TEST--
  2. foreach with Iterator.
  3. --FILE--
  4. <?php
  5. class MealIterator implements Iterator {
  6. private $pos=0;
  7. private $myContent=array("breakfast", "lunch", "dinner");
  8. public function valid() {
  9. global $indent;
  10. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  11. return $this->pos<3;
  12. }
  13. public function next() {
  14. global $indent;
  15. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  16. return $this->myContent[$this->pos++];
  17. }
  18. public function rewind() {
  19. global $indent;
  20. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  21. $this->pos=0;
  22. }
  23. public function current() {
  24. global $indent;
  25. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  26. return $this->myContent[$this->pos];
  27. }
  28. public function key() {
  29. global $indent;
  30. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  31. return "meal " . $this->pos;
  32. }
  33. }
  34. $f = new MealIterator;
  35. var_dump($f);
  36. echo "-----( Simple iteration: )-----\n";
  37. foreach ($f as $k=>$v) {
  38. echo "$k => $v\n";
  39. }
  40. $f->rewind();
  41. $indent = " ";
  42. echo "\n\n\n-----( Nested iteration: )-----\n";
  43. $count=1;
  44. foreach ($f as $k=>$v) {
  45. echo "\nTop level " . $count++ . ": \n";
  46. echo "$k => $v\n";
  47. $indent = " ";
  48. foreach ($f as $k=>$v) {
  49. echo " $k => $v\n";
  50. }
  51. $indent = " ";
  52. }
  53. ?>
  54. ===DONE===
  55. --EXPECTF--
  56. object(MealIterator)#%d (2) {
  57. ["pos":"MealIterator":private]=>
  58. int(0)
  59. ["myContent":"MealIterator":private]=>
  60. array(3) {
  61. [0]=>
  62. string(9) "breakfast"
  63. [1]=>
  64. string(5) "lunch"
  65. [2]=>
  66. string(6) "dinner"
  67. }
  68. }
  69. -----( Simple iteration: )-----
  70. --> MealIterator::rewind (0)
  71. --> MealIterator::valid (0)
  72. --> MealIterator::current (0)
  73. --> MealIterator::key (0)
  74. meal 0 => breakfast
  75. --> MealIterator::next (0)
  76. --> MealIterator::valid (1)
  77. --> MealIterator::current (1)
  78. --> MealIterator::key (1)
  79. meal 1 => lunch
  80. --> MealIterator::next (1)
  81. --> MealIterator::valid (2)
  82. --> MealIterator::current (2)
  83. --> MealIterator::key (2)
  84. meal 2 => dinner
  85. --> MealIterator::next (2)
  86. --> MealIterator::valid (3)
  87. --> MealIterator::rewind (3)
  88. -----( Nested iteration: )-----
  89. --> MealIterator::rewind (0)
  90. --> MealIterator::valid (0)
  91. --> MealIterator::current (0)
  92. --> MealIterator::key (0)
  93. Top level 1:
  94. meal 0 => breakfast
  95. --> MealIterator::rewind (0)
  96. --> MealIterator::valid (0)
  97. --> MealIterator::current (0)
  98. --> MealIterator::key (0)
  99. meal 0 => breakfast
  100. --> MealIterator::next (0)
  101. --> MealIterator::valid (1)
  102. --> MealIterator::current (1)
  103. --> MealIterator::key (1)
  104. meal 1 => lunch
  105. --> MealIterator::next (1)
  106. --> MealIterator::valid (2)
  107. --> MealIterator::current (2)
  108. --> MealIterator::key (2)
  109. meal 2 => dinner
  110. --> MealIterator::next (2)
  111. --> MealIterator::valid (3)
  112. --> MealIterator::next (3)
  113. Notice: Undefined offset: 3 in %s on line %d
  114. --> MealIterator::valid (4)
  115. ===DONE===