Deprecations in PHP 8.2
As it is common with every minor update in PHP framework some deprecations are added. Deprecations often are a source of nuisance and frustration. However, it’s important to realise they are actually very helpful. So if you’re already feeling frustrated, maybe it’s good to take a look at that post first. Today, I want to focus on one deprecation in particular in PHP 8.2: deprecated dynamic properties.
Dynamic Properties of PHP
So first things first, what are dynamic properties exactly? Well, they are properties that aren’t present on a class’ definition, but are set on objects of those classes dynamically, at runtime.
PHP used to be a very dynamic language, but has been moving away from that mindset for a while now. Personally I think it’s a good thing to embrace stricter rules and rely on static analysis wherever possible, as I find it leads to writing better code.
Deprecations weigh down PHP
I can imagine developers who relied on dynamic properties, who are less happy with this change. If you’re in that group, you might find it useful to take a closer look into static analysis. You can check out my Road to PHP: Static Analysisseries if you want to learn more!
If you’re willing to invest some time in figuring out static analysis, I’m fairly certain that most of you won’t ever want to return back to the mess that is a dynamic programming language. With PHP we’re lucky that both options are available and that you can migrate gradually towards a stricter type system.
So, yes: this deprecation might be a little painful, but I believe it’s for the best of the language to do so. And remember that it won’t be a fatal error until PHP 9.0, so there’s plenty of time to deal with it.
Implementing __get
and __set
still works!
You might be panicking at this point, because dynamic properties are a big part of meta programming in PHP — many frameworks rely on it!
Not to worry: this new deprecation won’t affect any class that implements __get
and __set
. Classes that implement these magic functions will keep working as intended