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

Abstract readonly Properties

PHP PHP 8.4+ Advanced
debt(d1/e1/b3/t3)
d1 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'caught instantly (compiler/syntax error)' (d1). Using abstract readonly in PHP < 8.4 is a syntax error caught immediately by the runtime. Implementing with a non-readonly property is a type/compile-time error. PHPStan and Psalm (listed tools) can also catch misuse statically, but the primary detection is the PHP engine itself at parse/compile time.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix states: declare 'abstract readonly Type $property' in abstract classes. Correcting a wrong implementation (non-readonly property or wrong PHP version) is a single-line declaration change per property, grounding directly in the quick_fix guidance.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax (one component pays)' (b3). The concept applies to abstract classes using PHP 8.4+ features — its reach is limited to classes opting into this pattern. It imposes a PHP version floor (8.4) on those classes and their implementors, but does not propagate a burden across the whole codebase. The applies_to scope (web, cli, queue-worker) is broad in principle, but only classes using the keyword are affected.

t3 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'minor surprise (one edge case)' (t3). The misconception field states developers may believe this existed before PHP 8.4, when it was only introduced in 8.4. There is also a common mistake of confusing with readonly classes (PHP 8.2). These are versioning and concept-boundary surprises, but they are relatively contained and documented; a competent developer will discover them quickly via syntax errors or documentation.

About DEBT scoring →

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,
    ) {}
}

Added 23 Mar 2026
Views 73
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 2 pings T 1 ping F 0 pings S 3 pings S 1 ping M 2 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 17 Perplexity 10 Scrapy 8 Google 6 ChatGPT 5 Unknown AI 3 Majestic 3 Meta AI 3 Ahrefs 3 Claude 2 PetalBot 2
crawler 57 crawler_json 4 pre-tracking 1
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


✓ schema.org compliant