implicit_instantiation_001.phpt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. --TEST--
  2. Implicit object instantiation when accessing properties of non-object.
  3. --FILE--
  4. <?php
  5. class C {
  6. // These values get implicitly converted to objects
  7. public $boolFalse = false;
  8. public $emptyString = '';
  9. public $null = null;
  10. // These values do not get implicitly converted to objects
  11. public $boolTrue = true;
  12. public $nonEmptyString = 'hello';
  13. public $intZero = 0;
  14. }
  15. $c = new C;
  16. foreach($c as $name => $value) {
  17. echo "\n\n---( \$c->$name )---";
  18. echo "\n --> Attempting implicit conversion to object using increment...\n";
  19. $c->$name->prop++;
  20. $c->$name = $value; // reset value in case implicit conversion was successful
  21. echo "\n --> Attempting implicit conversion to object using assignment...\n";
  22. $c->$name->prop = "Implicit instantiation!";
  23. $c->$name = $value; // reset value in case implicit conversion was successful
  24. echo "\n --> Attempting implicit conversion to object using combined assignment...\n";
  25. $c->$name->prop .= " Implicit instantiation!";
  26. }
  27. echo "\n\n\n --> Resulting object:";
  28. var_dump($c);
  29. ?>
  30. --EXPECTF--
  31. ---( $c->boolFalse )---
  32. --> Attempting implicit conversion to object using increment...
  33. Warning: Creating default object from empty value in %s on line 18
  34. Notice: Undefined property: stdClass::$prop in %s on line 18
  35. --> Attempting implicit conversion to object using assignment...
  36. Warning: Creating default object from empty value in %s on line 22
  37. --> Attempting implicit conversion to object using combined assignment...
  38. Warning: Creating default object from empty value in %s on line 26
  39. Notice: Undefined property: stdClass::$prop in %s on line 26
  40. ---( $c->emptyString )---
  41. --> Attempting implicit conversion to object using increment...
  42. Warning: Creating default object from empty value in %s on line 18
  43. Notice: Undefined property: stdClass::$prop in %s on line 18
  44. --> Attempting implicit conversion to object using assignment...
  45. Warning: Creating default object from empty value in %s on line 22
  46. --> Attempting implicit conversion to object using combined assignment...
  47. Warning: Creating default object from empty value in %s on line 26
  48. Notice: Undefined property: stdClass::$prop in %s on line 26
  49. ---( $c->null )---
  50. --> Attempting implicit conversion to object using increment...
  51. Warning: Creating default object from empty value in %s on line 18
  52. Notice: Undefined property: stdClass::$prop in %s on line 18
  53. --> Attempting implicit conversion to object using assignment...
  54. Warning: Creating default object from empty value in %s on line 22
  55. --> Attempting implicit conversion to object using combined assignment...
  56. Warning: Creating default object from empty value in %s on line 26
  57. Notice: Undefined property: stdClass::$prop in %s on line 26
  58. ---( $c->boolTrue )---
  59. --> Attempting implicit conversion to object using increment...
  60. Warning: Attempt to %s property 'prop' of non-object in %s on line 18
  61. --> Attempting implicit conversion to object using assignment...
  62. Warning: Attempt to assign property 'prop' of non-object in %s on line 22
  63. --> Attempting implicit conversion to object using combined assignment...
  64. Warning: Attempt to assign property 'prop' of non-object in %s on line 26
  65. ---( $c->nonEmptyString )---
  66. --> Attempting implicit conversion to object using increment...
  67. Warning: Attempt to %s property 'prop' of non-object in %s on line 18
  68. --> Attempting implicit conversion to object using assignment...
  69. Warning: Attempt to assign property 'prop' of non-object in %s on line 22
  70. --> Attempting implicit conversion to object using combined assignment...
  71. Warning: Attempt to assign property 'prop' of non-object in %s on line 26
  72. ---( $c->intZero )---
  73. --> Attempting implicit conversion to object using increment...
  74. Warning: Attempt to %s property 'prop' of non-object in %s on line 18
  75. --> Attempting implicit conversion to object using assignment...
  76. Warning: Attempt to assign property 'prop' of non-object in %s on line 22
  77. --> Attempting implicit conversion to object using combined assignment...
  78. Warning: Attempt to assign property 'prop' of non-object in %s on line 26
  79. --> Resulting object:object(C)#%d (6) {
  80. ["boolFalse"]=>
  81. object(stdClass)#%d (1) {
  82. ["prop"]=>
  83. string(24) " Implicit instantiation!"
  84. }
  85. ["emptyString"]=>
  86. object(stdClass)#%d (1) {
  87. ["prop"]=>
  88. string(24) " Implicit instantiation!"
  89. }
  90. ["null"]=>
  91. object(stdClass)#%d (1) {
  92. ["prop"]=>
  93. string(24) " Implicit instantiation!"
  94. }
  95. ["boolTrue"]=>
  96. bool(true)
  97. ["nonEmptyString"]=>
  98. string(5) "hello"
  99. ["intZero"]=>
  100. int(0)
  101. }