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 in /codes/creatingObjectOnNull.php on line 3

Warning: Creating default object from empty value in /codes/creatingObjectOnNull.php on line 3
1

After

PHP Fatal error:  Uncaught Error: Attempt to assign property a on null in /codes/creatingObjectOnNull.php:3
Stack trace:
#0 {main}
  thrown in /codes/creatingObjectOnNull.php on line 3

Fatal error: Uncaught Error: Attempt to assign property a on null in /codes/creatingObjectOnNull.php:3
Stack trace:
#0 {main}
  thrown in /codes/creatingObjectOnNull.php on line 3

PHP version change

This behavior was deprecated in 7.3

This behavior changed in 8.0

Error Messages