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

Property Hooks (PHP 8.4)

php PHP 8.4+ Advanced
debt(d5/e3/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list only PHPStan as a tool, with automated flagged as 'no'. Misuse — such as putting complex logic in hooks or incorrectly accessing the backing store — won't be caught by a compiler or default linter; PHPStan with custom rules or careful review is needed. This is solidly a specialist-tool catch.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing simple getter/setter pairs with property hooks or extracting complex logic back to methods. This is a localised refactor within one class or component — not a one-line swap but not cross-cutting either. Slightly above e1 because it may touch multiple properties/methods within a class.

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

Closest to 'localised tax' (b3). Property hooks apply broadly (web, cli, queue-worker contexts) but misuse is contained to individual classes. Overloading hooks with complex logic creates a maintainability tax within those classes, but the rest of the codebase is largely unaffected. The scope is per-class rather than architectural.

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

Closest to 'notable trap' (t5). The misconception is well-documented: developers assume hooks replace all getter/setter methods, when they're only appropriate for simple validation/transformation. The common mistakes also highlight the non-obvious behaviour of backing store access ($this->property in a backed hook) and virtual property limitations. This is a documented gotcha most developers encounter when adopting the feature.

About DEBT scoring →

TL;DR

PHP 8.4 property hooks add get and set accessors directly to property declarations — eliminating the need for explicit getter/setter methods on simple value objects.

Explanation

public string $name { get { return $this->name; } set { $this->name = trim($value); } }. Hooks: get (called on read), set (called on write, receives $value). Virtual properties (no backing storage): public string $fullName { get => $this->first . ' ' . $this->last; }. Rules: properties with get hook are implicitly readonly from outside. set hook receives $value. Backed vs virtual: backed properties store a value, virtual derive from others. Abstract property hooks in abstract classes. Interaction with readonly: readonly properties can only have get hooks. Interface property hooks define contracts.

Common Misconception

Property hooks replace all getter/setter methods — they're best for simple validation/transformation. Complex behaviour with multiple methods still warrants a proper getter/setter or value object.

Why It Matters

Property hooks eliminate getter/setter boilerplate for common patterns like trimming, normalising, and formatting — reducing class size for simple value objects.

Common Mistakes

  • Using hooks for complex logic — keep hooks simple, use methods for complex transforms.
  • Accessing $this->property in a backed hook without understanding the backing store.
  • Not knowing virtual properties (get only, no backing) can't be set directly.

Code Examples

✗ Vulnerable
class User {
    private string $name;

    public function getName(): string { return $this->name; }
    public function setName(string $name): void {
        $this->name = trim($name);
    }
}
✓ Fixed
// PHP 8.4:
class User {
    public string $name {
        set => trim($value);
    }

    // Virtual property (no backing storage):
    public string $initials {
        get {
            return implode('', array_map(
                fn($word) => $word[0],
                explode(' ', $this->name)
            ));
        }
    }
}

Added 23 Mar 2026
Views 24
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 10 Google 3 Perplexity 3 Unknown AI 2 ChatGPT 1 Meta AI 1 Ahrefs 1
crawler 19 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Medium
⚡ Quick Fix
Replace simple getter/setter pairs with property hooks in PHP 8.4. Use set => trim($value) for simple transformations. Keep complex logic in methods.
📦 Applies To
PHP 8.4+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
public.*{.*get|public.*{.*set
Auto-detectable: ✗ No phpstan
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class

✓ schema.org compliant