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
WeakMapwhen 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
- Composer.json Validator Validate composer.json structure and common mistakes without running composer install.
- Legacy PHP Risk Checker Paste PHP source for a static scan that classifies removed APIs, deprecated calls, and security-sensitive leg…
- PHP Deprecated Checker Find deprecated functions and patterns in pasted PHP to prioritize modernization work.
- PHP Environment Compare Compare two PHP environment summaries to find directive and extension mismatches.
- PHP Modernization Roadmap Build an ordered migration stage list from your PHP version, framework, Composer, database API, and deploymen…
- PHP Version Compatibility Checker Scan pasted PHP for version-sensitive syntax and APIs to plan upgrades across PHP releases.
Related reading
- Composer Modernization Center Add Composer to legacy PHP projects, migrate includes to autoloading, set platform constraints, and replace a…
- PHP 5 to Modern PHP: Complete Incremental Migration Guide A deep, production-minded path from PHP 5.x codebases to supported PHP 8.x: removed extensions, charset, PDO,…
- PHP Security Modernization for Legacy Applications Upgrade inherited PHP security practices: prepared statements, password hashing, sessions, CSRF, XSS escaping…
- PHP 7 to PHP 8 Migration Guide Deep guide to PHP 8.0 breaking changes that matter for PHP 7 applications, with upgrade tactics through suppo…
- Inheriting a Legacy PHP Application A first-30-days playbook for developers handed an unfamiliar PHP codebase: runtime truth, risk triage, and sa…