New Features in PHP 8.2: Readonly Classes, DNF Types, Random Extension and Deprecation of Dynamic Properties
The article outlines the release of PHP 8.2, detailing its major additions such as readonly classes, DNF types, new standalone types (null, false, true), the Random extension, trait constants, and the deprecation of dynamic properties, while providing code examples and migration guidance.
The PHP development team announced that PHP 8.2.0 is now immediately available as the latest minor version of the PHP language.
PHP 8.2 introduces many improvements and new features, including:
Readonly classes
Disjunctive Normal Form (DNF) types
New standalone types: null , false , true
New “Random” extension
Constants in traits
Deprecation of dynamic properties
Marking a class as readonly adds a readonly modifier to every declared property and prevents the creation of dynamic properties. It is also impossible to enable them by using the #[AllowDynamicProperties] attribute; attempting to do so triggers a compile‑time error.
<?php
#[AllowDynamicProperties]
readonly class Foo {
}
// Fatal error: Cannot apply #[AllowDynamicProperties] to readonly class Foo
?>A readonly class can be extended only when the subclass is also declared as readonly.
The creation of dynamic properties is deprecated unless the class explicitly opts‑in with the #[\AllowDynamicProperties] attribute. stdClass still permits dynamic properties. The use of __get() / __set() magic methods is not affected by this change. To resolve deprecation warnings, you can:
Declare properties (preferred).
Add the #[\AllowDynamicProperties] attribute to the class (applies to all subclasses).
If you need to associate extra data with an object you do not own, use WeakMap .
Official changelog: https://www.php.net/ChangeLog-8.php#8.2.0
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.