Dynamic Properties

Dynamic Properties and AllowDynamicProperties

Assigning an undeclared property creates a dynamic property, and Subsection 4.9.2 showed the deprecation PHP 8.2 attached to it. It applies to every class except stdClass, classes that implement __get() and __set() (Subsection 4.9.9), and classes marked #[\AllowDynamicProperties]; the RFC plans to make it an Error in PHP 9. A readonly class never accepts one.

The opt-out attribute and the stdClass exemptionPHP
<?php
#[\AllowDynamicProperties]
final class LegacyCart { public array $items = []; }
$l = new LegacyCart();  $l->coupon = 'SAVE10';     // allowed by the attribute
$s = new stdClass();    $s->coupon = 'SAVE10';     // stdClass is always exempt
echo $l->coupon, ' ', $s->coupon, "\n";
Output
SAVE10 SAVE10

Treat the attribute as a migration aid for old code, not a design choice. Declare the property, use an array property for open-ended data, or attach data to someone else's object with a WeakMap.