Article

Dynamic Properties Deprecated in PHP 8.2

Why creating undeclared object properties warns in PHP 8.2, and how to declare properties or opt in deliberately.

As of PHP 8.2, creating dynamic properties on most classes is deprecated. Official note: migration82 deprecated features. stdClass still allows dynamic properties; __get/__set usage is unaffected.

What triggers it

Assigning to a property that was never declared on the class (or parent) emits Deprecated: Creation of dynamic property …. Hydrators, ORMs with fuzzy column maps, and “bag” DTOs are frequent sources.

# LEGACY
class Order {}
$o = new Order();
$o->total = 19.99; // deprecated in 8.2+

# MODERN — declare the shape
class Order {
    public float $total;
}
$o = new Order();
$o->total = 19.99;

# TRANSITIONAL — deliberate opt-in (prefer not to sprinkle widely)
#[\AllowDynamicProperties]
class LegacyRow {}

Remediation strategy

  • Declare typed properties for known fields (best).
  • Use #[\AllowDynamicProperties] only on truly open-ended legacy classes you do not own cleanly.
  • Consider WeakMap when attaching metadata to objects you do not control.

Error encyclopedia: Deprecated dynamic property. Scan with deprecated checker. Parent: deprecated features hub.

Related scanners: PHP Version Compatibility Checker · PHP Deprecated Checker.

Related tools

Related reading