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

Creation of dynamic property %s::$%s is deprecated

Description

When PHP uses an undefined property for a writing access, it used to create that property on the fly. The property is created with public visibility, not type nor default value.

Since PHP 8.2, this is a deprecated feature. In a later version, by default, dynamic properties will be forbidden, and activated case by case, using sdtClass or the #[AllowDynamicProperties] attribute.

Example

<?php

class X {
    public $property = 1;
}

$x = new X;
$x->property = 2;
// This is a dynamic property
$x->other = 3;

?>

Alternatives

  • Declare the property in the class definition.
  • Create an array and store the properties there.
  • Create a stdClass object and store the properties there.
  • Add the #[AllowDynamicProperties].

Changed Behavior

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