← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

readonly Classes (PHP 8.2)

php PHP 8.2+ Beginner

TL;DR

PHP 8.2 readonly classes make all promoted properties readonly automatically — the cleanest way to define immutable value objects and DTOs.

Explanation

readonly class Money { } makes all declared properties implicitly readonly — no need to mark each one. Rules: all properties must be typed. Cannot have untyped or static properties. Allows constructor promotion. Can extend readonly classes. Cannot extend non-readonly classes (or vice versa — asymmetric readonly inheritance breaks the contract). readonly classes implement all readonly property restrictions: write-once, no unset, requires type. PHP 8.3: readonly properties can be modified in __clone() for copy-with patterns. Ideal for: DTOs, value objects, request/response objects, command/query objects.

Common Misconception

readonly classes prevent all mutation — readonly prevents property writes, but objects inside properties can still be mutated (shallow immutability). Use clone + new values for copy-with.

Why It Matters

readonly classes eliminate property-by-property readonly annotation while enforcing immutability for entire value object families — cleaner and safer.

Common Mistakes

  • Trying to extend a non-readonly class from readonly class — fatal error.
  • Adding static properties to readonly class — not allowed.
  • Assuming deep immutability — nested objects are still mutable.

Code Examples

✗ Vulnerable
class UserDTO {
    public function __construct(
        public readonly int $id,
        public readonly string $name,
        public readonly string $email,
    ) {}
}
✓ Fixed
// PHP 8.2 — readonly class:
readonly class UserDTO {
    public function __construct(
        public int $id,
        public string $name,
        public string $email,
    ) {}

    // PHP 8.3 copy-with:
    public function withName(string $name): static {
        $clone = clone $this;
        $clone->name = $name; // Only in __clone in 8.3
        return $clone;
    }
}

Added 23 Mar 2026
Views 23
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S
No pings yet today
Amazonbot 9 Unknown AI 3 Perplexity 3 Google 2 ChatGPT 1 Ahrefs 1
crawler 16 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Convert value objects and DTOs to readonly classes. Remove individual readonly keywords — the class declaration covers all properties.
📦 Applies To
PHP 8.2+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
class.*public readonly|readonly class
Auto-detectable: ✓ Yes rector phpstan
🤖 AI Agent
Confidence: Low False Positives: Medium ✓ Auto-fixable Fix: Low Context: Class

✓ schema.org compliant