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-- 38Simple loop. 39string(10) "Original a" 40string(10) "Original b" 41string(10) "Original c" 42string(1) "a" 43string(10) "Original a" 44string(1) "b" 45string(10) "Original b" 46string(1) "c" 47string(10) "Original c" 48 49Check key and value after the loop. 50string(1) "c" 51string(10) "Original c" 52 53 54Object instantiated inside loop. 55string(10) "Original a" 56string(10) "Original b" 57string(10) "Original c" 58string(1) "a" 59string(10) "Original a" 60string(1) "b" 61string(10) "Original b" 62string(1) "c" 63string(10) "Original c" 64 65Check key and value after the loop. 66string(1) "c" 67string(10) "Original c" 68