Abstract readonly Properties
TL;DR
PHP 8.4 allows abstract readonly properties in abstract classes and interfaces — defining that implementations must provide a readonly property of a specific type.
Explanation
PHP 8.4 introduced abstract readonly properties. An abstract class or interface can declare abstract public readonly string $name; requiring any concrete implementation to provide a readonly property with that name and type. This is more expressive than an abstract getter method — it declares structural intent. Implementations must declare the property as readonly (not just any property). This enables value-object hierarchies and interface-driven readonly DTOs. Before PHP 8.4, this required abstract getter methods as a workaround.
Common Misconception
✗ Abstract readonly properties existed before PHP 8.4 — they were added in 8.4. Before that, abstract getter methods were the only option.
Why It Matters
Abstract readonly properties enable interface contracts for immutable value objects without requiring abstract getter method boilerplate.
Common Mistakes
- Trying to use abstract readonly in PHP < 8.4 — syntax error.
- Implementing with a non-readonly property — type error.
- Confusing with readonly classes (PHP 8.2) which make all properties readonly.
Code Examples
✗ Vulnerable
// PHP 8.3 workaround — method instead of property:
abstract class Entity {
abstract public function getId(): int;
}
✓ Fixed
// PHP 8.4:
abstract class Entity {
abstract public readonly int $id;
}
class User extends Entity {
public function __construct(
public readonly int $id,
public readonly string $name,
) {}
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
49
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
No pings yesterday
Amazonbot 16
Perplexity 10
ChatGPT 5
Unknown AI 3
Google 3
Majestic 2
Meta AI 2
Ahrefs 1
Also referenced
How they use it
crawler 39
crawler_json 2
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Low
⚡ Quick Fix
Declare abstract readonly Type $property in abstract classes for immutable property contracts. Requires PHP 8.4.
📦 Applies To
PHP 8.4+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
abstract readonly
Auto-detectable:
✗ No
phpstan
psalm
🤖 AI Agent
Confidence: Low
False Positives: Medium
✗ Manual fix
Fix: Low
Context: Class