In PHP 8.2, Dynamic Properties are deprecated, and will result in a fatal error from PHP 9 onwards.
Using Dynamic Properties on Classes running PHP 8.2 will lead to PHP Deprecated: Creation of dynamic property is deprecated
E_DEPRECATED warnings.
Now, while this is generally a bad OO practice to have public/dynamic properties in a Class, this question is not about the best OO practice but instead, how to make actual code that uses Dynamic Properties compatible with PHP 8.2 onwards.
How to make actual codebases that make use of Dynamic Properties compatible with the new behaviour?
2
Answers
As suggested by ADyson, the solution is to use the
#[AllowDynamicProperties]
attribute just above the class definition.This is a full example, as contained in this github repository that I've created to test this feature on Traits and Extended Classes
replace this:
for this: