Dynamic properties deprecated in PHP 8.2: migration experience
PHP 8.2 deprecated dynamic properties (setting undefined properties on objects), removed in 9.0. We had about 80 places in our codebase using them.
Sharing the migration experience.
The deprecation warnings in logs are a good starting point. Enable log_errors and watch for “Creation of dynamic property” messages. Then search the codebase for patterns that create them.
stdClass is exempt. Dynamic properties on stdClass still work. The deprecation is for user-defined classes only. Most JSON decode and DB query results use stdClass so those are safe.
The AllowDynamicProperties attribute on a class suppresses the deprecation. Use it as a temporary fix for code you cannot change immediately, but add a TODO to remove it before PHP 9.
The main migration path: add property declarations. For tests that set arbitrary properties on mocks, use mock expectations or add the property with a default null.
Legacy ActiveRecord-style ORMs and data mapper classes were the biggest source of dynamic properties in our case. Some required non-trivial refactoring to add explicit property declarations with nullable types.
Rector has an automated rule for this: AddAllowDynamicPropertiesAttributeRector. It does not fix the root problem but handles the deprecation mechanically while you plan the real fixes.
```php blocks are runnable.