foreachLoopIteratorAggregate.004.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. --TEST--
  2. Duplicate of zend test tests/classes/iterators_002.phpt without expected output from destructor
  3. --FILE--
  4. <?php
  5. class c_iter implements Iterator {
  6. private $obj;
  7. private $num = 0;
  8. function __construct($obj) {
  9. echo __METHOD__ . "\n";
  10. $this->obj = $obj;
  11. }
  12. function rewind(): void {
  13. echo __METHOD__ . "\n";
  14. $this->num = 0;
  15. }
  16. function valid(): bool {
  17. $more = $this->num < $this->obj->max;
  18. echo __METHOD__ . ' = ' .($more ? 'true' : 'false') . "\n";
  19. return $more;
  20. }
  21. function current(): mixed {
  22. echo __METHOD__ . "\n";
  23. return $this->num;
  24. }
  25. function next(): void {
  26. echo __METHOD__ . "\n";
  27. $this->num++;
  28. }
  29. function key(): mixed {
  30. echo __METHOD__ . "\n";
  31. switch($this->num) {
  32. case 0: return "1st";
  33. case 1: return "2nd";
  34. case 2: return "3rd";
  35. default: return "???";
  36. }
  37. }
  38. function __destruct() {
  39. }
  40. }
  41. class c implements IteratorAggregate {
  42. public $max = 3;
  43. function getIterator(): Traversable {
  44. echo __METHOD__ . "\n";
  45. return new c_iter($this);
  46. }
  47. function __destruct() {
  48. }
  49. }
  50. $t = new c();
  51. foreach($t as $k => $v) {
  52. foreach($t as $w) {
  53. echo "double:$v:$w\n";
  54. break;
  55. }
  56. }
  57. unset($t);
  58. ?>
  59. --EXPECT--
  60. c::getIterator
  61. c_iter::__construct
  62. c_iter::rewind
  63. c_iter::valid = true
  64. c_iter::current
  65. c_iter::key
  66. c::getIterator
  67. c_iter::__construct
  68. c_iter::rewind
  69. c_iter::valid = true
  70. c_iter::current
  71. double:0:0
  72. c_iter::next
  73. c_iter::valid = true
  74. c_iter::current
  75. c_iter::key
  76. c::getIterator
  77. c_iter::__construct
  78. c_iter::rewind
  79. c_iter::valid = true
  80. c_iter::current
  81. double:1:0
  82. c_iter::next
  83. c_iter::valid = true
  84. c_iter::current
  85. c_iter::key
  86. c::getIterator
  87. c_iter::__construct
  88. c_iter::rewind
  89. c_iter::valid = true
  90. c_iter::current
  91. double:2:0
  92. c_iter::next
  93. c_iter::valid = false