1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
--TEST--
Implicit object instantiation when accessing properties of non-object.
--FILE--
<?php
class C {
// These values get implicitly converted to objects
public $boolFalse = false;
public $emptyString = '';
public $null = null;
// These values do not get implicitly converted to objects
public $boolTrue = true;
public $nonEmptyString = 'hello';
public $intZero = 0;
}
$c = new C;
foreach($c as $name => $value) {
echo "\n\n---( \$c->$name )---";
echo "\n --> Attempting implicit conversion to object using increment...\n";
$c->$name->prop++;
$c->$name = $value; // reset value in case implicit conversion was successful
echo "\n --> Attempting implicit conversion to object using assignment...\n";
$c->$name->prop = "Implicit instantiation!";
$c->$name = $value; // reset value in case implicit conversion was successful
echo "\n --> Attempting implicit conversion to object using combined assignment...\n";
$c->$name->prop .= " Implicit instantiation!";
}
echo "\n\n\n --> Resulting object:";
var_dump($c);
?>
--EXPECTF--
---( $c->boolFalse )---
--> Attempting implicit conversion to object using increment...
Warning: Creating default object from empty value in %s on line 18
Notice: Undefined property: stdClass::$prop in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Creating default object from empty value in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Creating default object from empty value in %s on line 26
Notice: Undefined property: stdClass::$prop in %s on line 26
---( $c->emptyString )---
--> Attempting implicit conversion to object using increment...
Warning: Creating default object from empty value in %s on line 18
Notice: Undefined property: stdClass::$prop in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Creating default object from empty value in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Creating default object from empty value in %s on line 26
Notice: Undefined property: stdClass::$prop in %s on line 26
---( $c->null )---
--> Attempting implicit conversion to object using increment...
Warning: Creating default object from empty value in %s on line 18
Notice: Undefined property: stdClass::$prop in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Creating default object from empty value in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Creating default object from empty value in %s on line 26
Notice: Undefined property: stdClass::$prop in %s on line 26
---( $c->boolTrue )---
--> Attempting implicit conversion to object using increment...
Warning: Attempt to %s property of non-object in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Attempt to assign property of non-object in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Attempt to assign property of non-object in %s on line 26
---( $c->nonEmptyString )---
--> Attempting implicit conversion to object using increment...
Warning: Attempt to %s property of non-object in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Attempt to assign property of non-object in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Attempt to assign property of non-object in %s on line 26
---( $c->intZero )---
--> Attempting implicit conversion to object using increment...
Warning: Attempt to %s property of non-object in %s on line 18
--> Attempting implicit conversion to object using assignment...
Warning: Attempt to assign property of non-object in %s on line 22
--> Attempting implicit conversion to object using combined assignment...
Warning: Attempt to assign property of non-object in %s on line 26
--> Resulting object:object(C)#%d (6) {
[%u|b%"boolFalse"]=>
object(stdClass)#%d (1) {
[%u|b%"prop"]=>
%unicode|string%(24) " Implicit instantiation!"
}
[%u|b%"emptyString"]=>
object(stdClass)#%d (1) {
[%u|b%"prop"]=>
%unicode|string%(24) " Implicit instantiation!"
}
[%u|b%"null"]=>
object(stdClass)#%d (1) {
[%u|b%"prop"]=>
%unicode|string%(24) " Implicit instantiation!"
}
[%u|b%"boolTrue"]=>
bool(true)
[%u|b%"nonEmptyString"]=>
%unicode|string%(5) "hello"
[%u|b%"intZero"]=>
int(0)
}
|