foreachLoopIterator.001.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(): bool {
  9. global $indent;
  10. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  11. return $this->pos<3;
  12. }
  13. public function next(): void {
  14. global $indent;
  15. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  16. $this->myContent[$this->pos++];
  17. }
  18. public function rewind(): void {
  19. global $indent;
  20. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  21. $this->pos=0;
  22. }
  23. public function current(): mixed {
  24. global $indent;
  25. echo "$indent--> " . __METHOD__ . " ($this->pos)\n";
  26. return $this->myContent[$this->pos];
  27. }
  28. public function key(): mixed {
  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. --EXPECTF--
  55. object(MealIterator)#%d (2) {
  56. ["pos":"MealIterator":private]=>
  57. int(0)
  58. ["myContent":"MealIterator":private]=>
  59. array(3) {
  60. [0]=>
  61. string(9) "breakfast"
  62. [1]=>
  63. string(5) "lunch"
  64. [2]=>
  65. string(6) "dinner"
  66. }
  67. }
  68. -----( Simple iteration: )-----
  69. --> MealIterator::rewind (0)
  70. --> MealIterator::valid (0)
  71. --> MealIterator::current (0)
  72. --> MealIterator::key (0)
  73. meal 0 => breakfast
  74. --> MealIterator::next (0)
  75. --> MealIterator::valid (1)
  76. --> MealIterator::current (1)
  77. --> MealIterator::key (1)
  78. meal 1 => lunch
  79. --> MealIterator::next (1)
  80. --> MealIterator::valid (2)
  81. --> MealIterator::current (2)
  82. --> MealIterator::key (2)
  83. meal 2 => dinner
  84. --> MealIterator::next (2)
  85. --> MealIterator::valid (3)
  86. --> MealIterator::rewind (3)
  87. -----( Nested iteration: )-----
  88. --> MealIterator::rewind (0)
  89. --> MealIterator::valid (0)
  90. --> MealIterator::current (0)
  91. --> MealIterator::key (0)
  92. Top level 1:
  93. meal 0 => breakfast
  94. --> MealIterator::rewind (0)
  95. --> MealIterator::valid (0)
  96. --> MealIterator::current (0)
  97. --> MealIterator::key (0)
  98. meal 0 => breakfast
  99. --> MealIterator::next (0)
  100. --> MealIterator::valid (1)
  101. --> MealIterator::current (1)
  102. --> MealIterator::key (1)
  103. meal 1 => lunch
  104. --> MealIterator::next (1)
  105. --> MealIterator::valid (2)
  106. --> MealIterator::current (2)
  107. --> MealIterator::key (2)
  108. meal 2 => dinner
  109. --> MealIterator::next (2)
  110. --> MealIterator::valid (3)
  111. --> MealIterator::next (3)
  112. Warning: Undefined array key 3 in %s on line %d
  113. --> MealIterator::valid (4)