foreachLoopObjects.005.phpt 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. --TEST--
  2. Foreach loop tests - removing properties before and after the current property 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 "\nRemoving properties before the current element from an iterated object.\n";
  13. $obj = new C;
  14. $count=0;
  15. foreach ($obj as $v) {
  16. if ($v==$obj->a) {
  17. unset($obj->c);
  18. }
  19. var_dump($v);
  20. if (++$count>10) {
  21. echo "Loop detected.\n";
  22. break;
  23. }
  24. }
  25. var_dump($obj);
  26. echo "\nRemoving properties before the current element from an iterated object.\n";
  27. $obj = new C;
  28. foreach ($obj as $v) {
  29. if ($v==$obj->b) {
  30. unset($obj->a);
  31. }
  32. var_dump($v);
  33. if (++$count>10) {
  34. echo "Loop detected.\n";
  35. break;
  36. }
  37. }
  38. var_dump($obj);
  39. ?>
  40. --EXPECTF--
  41. Removing properties before the current element from an iterated object.
  42. string(10) "Original a"
  43. string(10) "Original b"
  44. string(10) "Original d"
  45. string(10) "Original e"
  46. object(C)#%d (4) {
  47. ["a"]=>
  48. string(10) "Original a"
  49. ["b"]=>
  50. string(10) "Original b"
  51. ["d"]=>
  52. string(10) "Original d"
  53. ["e"]=>
  54. string(10) "Original e"
  55. }
  56. Removing properties before the current element from an iterated object.
  57. string(10) "Original a"
  58. string(10) "Original b"
  59. string(10) "Original c"
  60. string(10) "Original d"
  61. string(10) "Original e"
  62. object(C)#%d (4) {
  63. ["b"]=>
  64. string(10) "Original b"
  65. ["c"]=>
  66. string(10) "Original c"
  67. ["d"]=>
  68. string(10) "Original d"
  69. ["e"]=>
  70. string(10) "Original e"
  71. }