foreachLoopObjects.004.phpt 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. --TEST--
  2. Foreach loop tests - Removing the current element from an iterated object.
  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 the current element from an iterated object.\n";
  13. $obj = new C;
  14. $count=0;
  15. foreach ($obj as $v) {
  16. if ($v==$obj->b) {
  17. unset($obj->b);
  18. }
  19. var_dump($v);
  20. if (++$count>10) {
  21. echo "Loop detected.\n";
  22. break;
  23. }
  24. }
  25. var_dump($obj);
  26. ?>
  27. ===DONE===
  28. --EXPECTF--
  29. Removing the current element from an iterated object.
  30. string(10) "Original a"
  31. string(10) "Original b"
  32. Notice: Undefined property: C::$b in %s on line %d
  33. string(10) "Original c"
  34. Notice: Undefined property: C::$b in %s on line %d
  35. string(10) "Original d"
  36. Notice: Undefined property: C::$b in %s on line %d
  37. string(10) "Original e"
  38. object(C)#%d (4) {
  39. ["a"]=>
  40. string(10) "Original a"
  41. ["c"]=>
  42. string(10) "Original c"
  43. ["d"]=>
  44. string(10) "Original d"
  45. ["e"]=>
  46. string(10) "Original e"
  47. }
  48. ===DONE===