You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
2.3 KiB

  1. --TEST--
  2. Foreach loop tests - substituting the entire iterated entity during the loop.
  3. --FILE--
  4. <?php
  5. class C {
  6. public $a = "Original a";
  7. public $b = "Original b";
  8. public $c = "Original c";
  9. public $d = "Original d";
  10. public $e = "Original e";
  11. }
  12. echo "\nSubstituting the iterated object for a different object.\n";
  13. $obj = new C;
  14. $obj2 = new stdclass;
  15. $obj2->a = "new a";
  16. $obj2->b = "new b";
  17. $obj2->c = "new c";
  18. $obj2->d = "new d";
  19. $obj2->e = "new e";
  20. $obj2->f = "new f";
  21. $ref = &$obj;
  22. $count=0;
  23. foreach ($obj as $v) {
  24. var_dump($v);
  25. if ($v==$obj->b) {
  26. $ref=$obj2;
  27. }
  28. if (++$count>10) {
  29. echo "Loop detected.\n";
  30. break;
  31. }
  32. }
  33. var_dump($obj);
  34. echo "\nSubstituting the iterated object for an array.\n";
  35. $obj = new C;
  36. $a = array(1,2,3,4,5,6,7,8);
  37. $ref = &$obj;
  38. $count=0;
  39. foreach ($obj as $v) {
  40. var_dump($v);
  41. if ($v==="Original b") {
  42. $ref=$a;
  43. }
  44. if (++$count>10) {
  45. echo "Loop detected.\n";
  46. break;
  47. }
  48. }
  49. var_dump($obj);
  50. echo "\nSubstituting the iterated array for an object.\n";
  51. $a = array(1,2,3,4,5,6,7,8);
  52. $obj = new C;
  53. $ref = &$a;
  54. $count=0;
  55. foreach ($a as $v) {
  56. var_dump($v);
  57. if ($v===2) {
  58. $ref=$obj;
  59. }
  60. if (++$count>10) {
  61. echo "Loop detected.\n";
  62. break;
  63. }
  64. }
  65. var_dump($obj);
  66. ?>
  67. --EXPECTF--
  68. Substituting the iterated object for a different object.
  69. string(10) "Original a"
  70. string(10) "Original b"
  71. string(5) "new a"
  72. string(5) "new b"
  73. string(5) "new c"
  74. string(5) "new d"
  75. string(5) "new e"
  76. string(5) "new f"
  77. object(stdClass)#%d (6) {
  78. ["a"]=>
  79. string(5) "new a"
  80. ["b"]=>
  81. string(5) "new b"
  82. ["c"]=>
  83. string(5) "new c"
  84. ["d"]=>
  85. string(5) "new d"
  86. ["e"]=>
  87. string(5) "new e"
  88. ["f"]=>
  89. string(5) "new f"
  90. }
  91. Substituting the iterated object for an array.
  92. string(10) "Original a"
  93. string(10) "Original b"
  94. int(1)
  95. int(2)
  96. int(3)
  97. int(4)
  98. int(5)
  99. int(6)
  100. int(7)
  101. int(8)
  102. array(8) {
  103. [0]=>
  104. int(1)
  105. [1]=>
  106. int(2)
  107. [2]=>
  108. int(3)
  109. [3]=>
  110. int(4)
  111. [4]=>
  112. int(5)
  113. [5]=>
  114. int(6)
  115. [6]=>
  116. int(7)
  117. [7]=>
  118. int(8)
  119. }
  120. Substituting the iterated array for an object.
  121. int(1)
  122. int(2)
  123. string(10) "Original a"
  124. string(10) "Original b"
  125. string(10) "Original c"
  126. string(10) "Original d"
  127. string(10) "Original e"
  128. object(C)#%d (5) {
  129. ["a"]=>
  130. string(10) "Original a"
  131. ["b"]=>
  132. string(10) "Original b"
  133. ["c"]=>
  134. string(10) "Original c"
  135. ["d"]=>
  136. string(10) "Original d"
  137. ["e"]=>
  138. string(10) "Original e"
  139. }