Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;

?>

Alternatives

  • Explicitly create the object before accessing its properties.
  • Cast an array to (object).
  • Instanciate a stdClass.

In more recent PHP versions, this error message is now :ref:attempt-to-assign-property-"%s"-on-null.

Changed Behavior

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