Creating default object from empty value

Description

This deprecation message is emitted in PHP 7.x to signal that an object was created without instantiation. In the illustration code, $x does not exist before assigning it a property a, and a value. The created object is stdClass.

This error is specific to the creation of the variable: if $x was set with null, a different message was emitted.

Example

<?php

$x->a = 1;

?>

Solutions

  • Explicitly create the object before accessing its properties.

  • Cast an array to (object).

In more recent PHP versions, this error message is now Attempt to assign property “%s” on null.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See creatingObjectOnNull.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See creatingObjectOnNull.