1--TEST-- 2Foreach loop tests - visibility. 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 function doForEachC() { 14 echo "in C::doForEachC\n"; 15 foreach ($this as $k=>&$v) { 16 var_dump($v); 17 $v="changed.$k"; 18 } 19 } 20 21 static function doForEach($obj) { 22 echo "in C::doForEach\n"; 23 foreach ($obj as $k=>&$v) { 24 var_dump($v); 25 $v="changed.$k"; 26 } 27 } 28 29 function doForEachOnThis() { 30 echo "in C::doForEachOnThis\n"; 31 foreach ($this as $k=>&$v) { 32 var_dump($v); 33 $v="changed.$k"; 34 } 35 } 36 37} 38 39class D extends C { 40 41 private $f = "Original f"; 42 protected $g = "Original g"; 43 44 static function doForEach($obj) { 45 echo "in D::doForEach\n"; 46 foreach ($obj as $k=>&$v) { 47 var_dump($v); 48 $v="changed.$k"; 49 } 50 } 51 52 function doForEachOnThis() { 53 echo "in D::doForEachOnThis\n"; 54 foreach ($this as $k=>&$v) { 55 var_dump($v); 56 $v="changed.$k"; 57 } 58 } 59} 60 61class E extends D { 62 public $a = "Overridden a"; 63 public $b = "Overridden b"; 64 public $c = "Overridden c"; 65 protected $d = "Overridden d"; 66 private $e = "Overridden e"; 67 68 static function doForEach($obj) { 69 echo "in E::doForEach\n"; 70 foreach ($obj as $k=>&$v) { 71 var_dump($v); 72 $v="changed.$k"; 73 } 74 } 75 76 function doForEachOnThis() { 77 echo "in E::doForEachOnThis\n"; 78 foreach ($this as $k=>&$v) { 79 var_dump($v); 80 $v="changed.$k"; 81 } 82 } 83} 84 85echo "\n\nIterate over various generations from within overridden methods:\n"; 86echo "\n--> Using instance of C:\n"; 87$myC = new C; 88$myC->doForEachOnThis(); 89var_dump($myC); 90echo "\n--> Using instance of D:\n"; 91$myD = new D; 92$myD->doForEachOnThis(); 93var_dump($myD); 94echo "\n--> Using instance of E:\n"; 95$myE = new E; 96$myE->doForEachOnThis(); 97var_dump($myE); 98 99echo "\n\nIterate over various generations from within an inherited method:\n"; 100echo "\n--> Using instance of C:\n"; 101$myC = new C; 102$myC->doForEachC(); 103var_dump($myC); 104echo "\n--> Using instance of D:\n"; 105$myD = new D; 106$myD->doForEachC(); 107var_dump($myD); 108echo "\n--> Using instance of E:\n"; 109$myE = new E; 110$myE->doForEachC(); 111var_dump($myE); 112 113echo "\n\nIterate over various generations from within an overridden static method:\n"; 114echo "\n--> Using instance of C:\n"; 115$myC = new C; 116C::doForEach($myC); 117var_dump($myC); 118$myC = new C; 119D::doForEach($myC); 120var_dump($myC); 121$myC = new C; 122E::doForEach($myC); 123var_dump($myC); 124echo "\n--> Using instance of D:\n"; 125$myD = new D; 126C::doForEach($myD); 127var_dump($myD); 128$myD = new D; 129D::doForEach($myD); 130var_dump($myD); 131$myD = new D; 132E::doForEach($myD); 133var_dump($myD); 134echo "\n--> Using instance of E:\n"; 135$myE = new E; 136C::doForEach($myE); 137var_dump($myE); 138$myE = new E; 139D::doForEach($myE); 140var_dump($myE); 141$myE = new E; 142E::doForEach($myE); 143var_dump($myE); 144 145 146echo "\n\nIterate over various generations from outside the object:\n"; 147echo "\n--> Using instance of C:\n"; 148$myC = new C; 149foreach ($myC as $k=>&$v) { 150 var_dump($v); 151 $v="changed.$k"; 152} 153var_dump($myC); 154echo "\n--> Using instance of D:\n"; 155$myD = new D; 156foreach ($myD as $k=>&$v) { 157 var_dump($v); 158 $v="changed.$k"; 159} 160var_dump($myD); 161echo "\n--> Using instance of E:\n"; 162$myE = new E; 163foreach ($myE as $k=>&$v) { 164 var_dump($v); 165 $v="changed.$k"; 166} 167var_dump($myE); 168?> 169===DONE=== 170--EXPECTF-- 171Iterate over various generations from within overridden methods: 172 173--> Using instance of C: 174in C::doForEachOnThis 175string(10) "Original a" 176string(10) "Original b" 177string(10) "Original c" 178string(10) "Original d" 179string(10) "Original e" 180object(C)#%d (5) { 181 ["a"]=> 182 string(9) "changed.a" 183 ["b"]=> 184 string(9) "changed.b" 185 ["c"]=> 186 string(9) "changed.c" 187 ["d":protected]=> 188 string(9) "changed.d" 189 ["e":"C":private]=> 190 string(9) "changed.e" 191} 192 193--> Using instance of D: 194in D::doForEachOnThis 195string(10) "Original f" 196string(10) "Original g" 197string(10) "Original a" 198string(10) "Original b" 199string(10) "Original c" 200string(10) "Original d" 201object(D)#%d (7) { 202 ["f":"D":private]=> 203 string(9) "changed.f" 204 ["g":protected]=> 205 string(9) "changed.g" 206 ["a"]=> 207 string(9) "changed.a" 208 ["b"]=> 209 string(9) "changed.b" 210 ["c"]=> 211 string(9) "changed.c" 212 ["d":protected]=> 213 string(9) "changed.d" 214 ["e":"C":private]=> 215 string(10) "Original e" 216} 217 218--> Using instance of E: 219in E::doForEachOnThis 220string(12) "Overridden a" 221string(12) "Overridden b" 222string(12) "Overridden c" 223string(12) "Overridden d" 224string(12) "Overridden e" 225string(10) "Original g" 226object(E)#%d (8) { 227 ["a"]=> 228 string(9) "changed.a" 229 ["b"]=> 230 string(9) "changed.b" 231 ["c"]=> 232 string(9) "changed.c" 233 ["d":protected]=> 234 string(9) "changed.d" 235 ["e":"E":private]=> 236 string(9) "changed.e" 237 ["f":"D":private]=> 238 string(10) "Original f" 239 ["g":protected]=> 240 string(9) "changed.g" 241 ["e":"C":private]=> 242 string(10) "Original e" 243} 244 245 246Iterate over various generations from within an inherited method: 247 248--> Using instance of C: 249in C::doForEachC 250string(10) "Original a" 251string(10) "Original b" 252string(10) "Original c" 253string(10) "Original d" 254string(10) "Original e" 255object(C)#%d (5) { 256 ["a"]=> 257 string(9) "changed.a" 258 ["b"]=> 259 string(9) "changed.b" 260 ["c"]=> 261 string(9) "changed.c" 262 ["d":protected]=> 263 string(9) "changed.d" 264 ["e":"C":private]=> 265 string(9) "changed.e" 266} 267 268--> Using instance of D: 269in C::doForEachC 270string(10) "Original g" 271string(10) "Original a" 272string(10) "Original b" 273string(10) "Original c" 274string(10) "Original d" 275string(10) "Original e" 276object(D)#%d (7) { 277 ["f":"D":private]=> 278 string(10) "Original f" 279 ["g":protected]=> 280 string(9) "changed.g" 281 ["a"]=> 282 string(9) "changed.a" 283 ["b"]=> 284 string(9) "changed.b" 285 ["c"]=> 286 string(9) "changed.c" 287 ["d":protected]=> 288 string(9) "changed.d" 289 ["e":"C":private]=> 290 string(9) "changed.e" 291} 292 293--> Using instance of E: 294in C::doForEachC 295string(12) "Overridden a" 296string(12) "Overridden b" 297string(12) "Overridden c" 298string(12) "Overridden d" 299string(10) "Original g" 300string(10) "Original e" 301object(E)#%d (8) { 302 ["a"]=> 303 string(9) "changed.a" 304 ["b"]=> 305 string(9) "changed.b" 306 ["c"]=> 307 string(9) "changed.c" 308 ["d":protected]=> 309 string(9) "changed.d" 310 ["e":"E":private]=> 311 string(12) "Overridden e" 312 ["f":"D":private]=> 313 string(10) "Original f" 314 ["g":protected]=> 315 string(9) "changed.g" 316 ["e":"C":private]=> 317 string(9) "changed.e" 318} 319 320 321Iterate over various generations from within an overridden static method: 322 323--> Using instance of C: 324in C::doForEach 325string(10) "Original a" 326string(10) "Original b" 327string(10) "Original c" 328string(10) "Original d" 329string(10) "Original e" 330object(C)#%d (5) { 331 ["a"]=> 332 string(9) "changed.a" 333 ["b"]=> 334 string(9) "changed.b" 335 ["c"]=> 336 string(9) "changed.c" 337 ["d":protected]=> 338 string(9) "changed.d" 339 ["e":"C":private]=> 340 string(9) "changed.e" 341} 342in D::doForEach 343string(10) "Original a" 344string(10) "Original b" 345string(10) "Original c" 346string(10) "Original d" 347object(C)#%d (5) { 348 ["a"]=> 349 string(9) "changed.a" 350 ["b"]=> 351 string(9) "changed.b" 352 ["c"]=> 353 string(9) "changed.c" 354 ["d":protected]=> 355 string(9) "changed.d" 356 ["e":"C":private]=> 357 string(10) "Original e" 358} 359in E::doForEach 360string(10) "Original a" 361string(10) "Original b" 362string(10) "Original c" 363string(10) "Original d" 364object(C)#%d (5) { 365 ["a"]=> 366 string(9) "changed.a" 367 ["b"]=> 368 string(9) "changed.b" 369 ["c"]=> 370 string(9) "changed.c" 371 ["d":protected]=> 372 string(9) "changed.d" 373 ["e":"C":private]=> 374 string(10) "Original e" 375} 376 377--> Using instance of D: 378in C::doForEach 379string(10) "Original g" 380string(10) "Original a" 381string(10) "Original b" 382string(10) "Original c" 383string(10) "Original d" 384string(10) "Original e" 385object(D)#%d (7) { 386 ["f":"D":private]=> 387 string(10) "Original f" 388 ["g":protected]=> 389 string(9) "changed.g" 390 ["a"]=> 391 string(9) "changed.a" 392 ["b"]=> 393 string(9) "changed.b" 394 ["c"]=> 395 string(9) "changed.c" 396 ["d":protected]=> 397 string(9) "changed.d" 398 ["e":"C":private]=> 399 string(9) "changed.e" 400} 401in D::doForEach 402string(10) "Original f" 403string(10) "Original g" 404string(10) "Original a" 405string(10) "Original b" 406string(10) "Original c" 407string(10) "Original d" 408object(D)#%d (7) { 409 ["f":"D":private]=> 410 string(9) "changed.f" 411 ["g":protected]=> 412 string(9) "changed.g" 413 ["a"]=> 414 string(9) "changed.a" 415 ["b"]=> 416 string(9) "changed.b" 417 ["c"]=> 418 string(9) "changed.c" 419 ["d":protected]=> 420 string(9) "changed.d" 421 ["e":"C":private]=> 422 string(10) "Original e" 423} 424in E::doForEach 425string(10) "Original g" 426string(10) "Original a" 427string(10) "Original b" 428string(10) "Original c" 429string(10) "Original d" 430object(D)#%d (7) { 431 ["f":"D":private]=> 432 string(10) "Original f" 433 ["g":protected]=> 434 string(9) "changed.g" 435 ["a"]=> 436 string(9) "changed.a" 437 ["b"]=> 438 string(9) "changed.b" 439 ["c"]=> 440 string(9) "changed.c" 441 ["d":protected]=> 442 string(9) "changed.d" 443 ["e":"C":private]=> 444 string(10) "Original e" 445} 446 447--> Using instance of E: 448in C::doForEach 449string(12) "Overridden a" 450string(12) "Overridden b" 451string(12) "Overridden c" 452string(12) "Overridden d" 453string(10) "Original g" 454string(10) "Original e" 455object(E)#%d (8) { 456 ["a"]=> 457 string(9) "changed.a" 458 ["b"]=> 459 string(9) "changed.b" 460 ["c"]=> 461 string(9) "changed.c" 462 ["d":protected]=> 463 string(9) "changed.d" 464 ["e":"E":private]=> 465 string(12) "Overridden e" 466 ["f":"D":private]=> 467 string(10) "Original f" 468 ["g":protected]=> 469 string(9) "changed.g" 470 ["e":"C":private]=> 471 string(9) "changed.e" 472} 473in D::doForEach 474string(12) "Overridden a" 475string(12) "Overridden b" 476string(12) "Overridden c" 477string(12) "Overridden d" 478string(10) "Original f" 479string(10) "Original g" 480object(E)#%d (8) { 481 ["a"]=> 482 string(9) "changed.a" 483 ["b"]=> 484 string(9) "changed.b" 485 ["c"]=> 486 string(9) "changed.c" 487 ["d":protected]=> 488 string(9) "changed.d" 489 ["e":"E":private]=> 490 string(12) "Overridden e" 491 ["f":"D":private]=> 492 string(9) "changed.f" 493 ["g":protected]=> 494 string(9) "changed.g" 495 ["e":"C":private]=> 496 string(10) "Original e" 497} 498in E::doForEach 499string(12) "Overridden a" 500string(12) "Overridden b" 501string(12) "Overridden c" 502string(12) "Overridden d" 503string(12) "Overridden e" 504string(10) "Original g" 505object(E)#%d (8) { 506 ["a"]=> 507 string(9) "changed.a" 508 ["b"]=> 509 string(9) "changed.b" 510 ["c"]=> 511 string(9) "changed.c" 512 ["d":protected]=> 513 string(9) "changed.d" 514 ["e":"E":private]=> 515 string(9) "changed.e" 516 ["f":"D":private]=> 517 string(10) "Original f" 518 ["g":protected]=> 519 string(9) "changed.g" 520 ["e":"C":private]=> 521 string(10) "Original e" 522} 523 524 525Iterate over various generations from outside the object: 526 527--> Using instance of C: 528string(10) "Original a" 529string(10) "Original b" 530string(10) "Original c" 531object(C)#%d (5) { 532 ["a"]=> 533 string(9) "changed.a" 534 ["b"]=> 535 string(9) "changed.b" 536 ["c"]=> 537 &string(9) "changed.c" 538 ["d":protected]=> 539 string(10) "Original d" 540 ["e":"C":private]=> 541 string(10) "Original e" 542} 543 544--> Using instance of D: 545string(10) "Original a" 546string(10) "Original b" 547string(10) "Original c" 548object(D)#%d (7) { 549 ["f":"D":private]=> 550 string(10) "Original f" 551 ["g":protected]=> 552 string(10) "Original g" 553 ["a"]=> 554 string(9) "changed.a" 555 ["b"]=> 556 string(9) "changed.b" 557 ["c"]=> 558 &string(9) "changed.c" 559 ["d":protected]=> 560 string(10) "Original d" 561 ["e":"C":private]=> 562 string(10) "Original e" 563} 564 565--> Using instance of E: 566string(12) "Overridden a" 567string(12) "Overridden b" 568string(12) "Overridden c" 569object(E)#%d (8) { 570 ["a"]=> 571 string(9) "changed.a" 572 ["b"]=> 573 string(9) "changed.b" 574 ["c"]=> 575 &string(9) "changed.c" 576 ["d":protected]=> 577 string(12) "Overridden d" 578 ["e":"E":private]=> 579 string(12) "Overridden e" 580 ["f":"D":private]=> 581 string(10) "Original f" 582 ["g":protected]=> 583 string(10) "Original g" 584 ["e":"C":private]=> 585 string(10) "Original e" 586} 587===DONE===