PHP Development

PHP 8.2: Readonly Classes, DNF Types, and What’s Deprecated

PHP 8.2 (released December 2022, widely deployed through 2023) continues the PHP 8.x refinement track. The headline features are modest compared to PHP 8.0’s landmark release, but readonly classes and DNF types are immediately useful, and several long-overdue deprecations clean up rough edges in the language.

Readonly Classes

PHP 8.1 introduced readonly properties. PHP 8.2 extends this to entire classes — all properties become implicitly readonly:

readonly class Money {
    public function __construct(
        public int    $amount,
        public string $currency,
    ) {}
}

$price = new Money(1999, 'USD');
$price->amount = 2499; // Fatal error: Cannot modify readonly property

Readonly classes are perfect for Value Objects and Data Transfer Objects — immutable data carriers that should never change after construction.

Disjunctive Normal Form (DNF) Types

DNF types combine intersection types (&) with union types (|), following DNF rules:

// A function that accepts something that implements both A and B, OR is null
function process((A&B)|null $value): void {
    if ($value === null) return;
    $value->doA();
    $value->doB();
}

The New Random Extension

PHP 8.2 introduces a new object-oriented random number generation API that makes randomness explicit, testable, and reproducible:

$rng = new RandomRandomizer(new RandomEngineMt19937(seed: 42));
$rng->getInt(1, 100);        // deterministic with seed
$rng->shuffleArray($items);  // reproducible shuffle

Deprecated: Dynamic Properties

PHP 8.2 deprecates creating properties dynamically on class instances (setting a property that was not declared). This was a common source of bugs. Use #[AllowDynamicProperties] on classes that legitimately need them, or declare all properties explicitly.

Deprecated: Partially Supported Callables

Callables like [$this, 'privateMethod'] passed to call_user_func() are deprecated when they are not accessible in the calling context. This closes a visibility loophole that allowed calling private methods from outside the class.

Fibers Are Now Stable

Fibers (PHP 8.1) are now battle-tested in production. ReactPHP and Amphp use them to implement coroutine-based async programming — a PHP analogue to JavaScript async/await without the language-level syntax.

Upgrading from PHP 8.1

Run php -d error_reporting=E_ALL path/to/script.php or use the Rector tool with the PHP 8.2 ruleset to automatically surface and fix deprecations. Most PHP 8.1 code runs on 8.2 without modification.

PHP 8.2 is a polishing release. The direction is clear: a more type-safe, predictable, and performant language. PHP 8.3 (December 2023) continues this trend with typed class constants and further improvements.

Share this post:
Copied!

Leave a Comment

Your email will not be published.

READY TO BUILD?

Let's Build Something
Amazing Together

Tell us about your project. We'll have a proposal in your inbox within 24 hours.

Free Consultation
NDA Available
Fixed-price Options
Dedicated PM