This article will cover what’s new in PHP 8.2 in detail — from its new features and improvements to deprecations and minor changes, we’ll go through them all.
PHP 8.1 introduced the readonly feature for class properties. Now, PHP 8.2 is adding support to declare the entire class as readonly.
If you declare a class as readonly, all its properties will automatically inherit the readonly feature. Thus, declaring a class readonly is the same as declaring every class property as readonly.
You can also declare a readonly class with no properties. Effectively, this prevents dynamic properties while still allowing child classes to declare their readonly properties explicitly.
PHP 8.2 also deprecates dynamic properties (more on that later). But you cannot prevent dynamic properties from being added to a class. However, doing so for a readonly class will only result in a Fatal Error.
PHP already includes scalar types like int, string, and bool. That was expanded in PHP 8.0 with the addition of union types, allowing values to be of different types. The same RFC also allowed using false and null as part of a union type — they weren’t allowed as standalone types, though.