1--TEST-- 2Foreach loop on objects - basic loop with just value and key => value. 3--FILE-- 4<?php 5 6class C { 7 public $a = "Original a"; 8 public $b = "Original b"; 9 public $c = "Original c"; 10 protected $d = "Original d"; 11 private $e = "Original e"; 12 13} 14 15echo "\n\nSimple loop.\n"; 16$obj = new C; 17foreach ($obj as $v) { 18 var_dump($v); 19} 20foreach ($obj as $k => $v) { 21 var_dump($k, $v); 22} 23echo "\nCheck key and value after the loop.\n"; 24var_dump($k, $v); 25 26 27echo "\n\nObject instantiated inside loop.\n"; 28foreach (new C as $v) { 29 var_dump($v); 30} 31foreach (new C as $k => $v) { 32 var_dump($k, $v); 33} 34echo "\nCheck key and value after the loop.\n"; 35var_dump($k, $v); 36?> 37--EXPECTF-- 38 39 40Simple loop. 41string(10) "Original a" 42string(10) "Original b" 43string(10) "Original c" 44string(1) "a" 45string(10) "Original a" 46string(1) "b" 47string(10) "Original b" 48string(1) "c" 49string(10) "Original c" 50 51Check key and value after the loop. 52string(1) "c" 53string(10) "Original c" 54 55 56Object instantiated inside loop. 57string(10) "Original a" 58string(10) "Original b" 59string(10) "Original c" 60string(1) "a" 61string(10) "Original a" 62string(1) "b" 63string(10) "Original b" 64string(1) "c" 65string(10) "Original c" 66 67Check key and value after the loop. 68string(1) "c" 69string(10) "Original c"