Creating Object On Null¶
Until PHP 8, it was possible to create an object just like a variable: simply by assigning a value to one property. The created object was of class stdClass, and could be used further.
In PHP 8.0, this is now a Fatal error. Later, undefined properties, also known as dynamic properties were deprecated, and will lead to a Fatal error in PHP 9.
PHP code¶
<?php
$x->a = 1;
print $x->a;
?>
Before¶
PHP Warning: Creating default object from empty value
Warning: Creating default object from empty value
1
After¶
PHP Fatal error: Uncaught Error: Attempt to assign property "a" on null
Fatal error: Uncaught Error: Attempt to assign property "a" on null
PHP version change¶
This behavior was deprecated in 7.3
This behavior changed in 8.0