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

Typed Properties (PHP 7.4)

PHP PHP 7.4+ Intermediate
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints.tools list names phpstan, psalm, and rector — all specialist static analysis tools. The common mistake of accessing a typed property before initialization won't be caught by a compiler error at declaration time, nor by a default linter, but PHPStan level 8+ and Psalm will flag uninitialized property access. At runtime it throws an Error, but only when that code path is hit.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix says: add type declarations to all class properties, use ?Type for nullable, initialize in constructor or use = default. This is a small, mechanical refactor within one component (the class file), not a cross-cutting change. Multiple properties may need updating but the pattern is uniform and rector can automate much of it.

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

Closest to 'localised tax' (b3). The applies_to contexts are wide (web, cli, queue-worker) but the burden is felt at the class level — each class pays the tax of correctly initializing its typed properties, while the rest of the codebase is largely unaffected. It doesn't reshape system architecture, but it does impose a persistent discipline on class authorship.

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

Closest to 'serious trap' (t7). The misconception field directly describes the trap: developers expect typed properties to behave like typed parameters (returning null when unset), but they actually throw an Error on uninitialized access. This contradicts the mental model from typed function parameters and from untyped properties in earlier PHP, making it a behavior that contradicts how a similar concept works elsewhere (t7).

About DEBT scoring →

TL;DR

PHP 7.4 typed properties add type declarations to class properties — public string $name — preventing type mismatches and enabling full static analysis of object state.

Explanation

PHP 7.4: class properties can be typed: public int $age; private string $name; protected ?User $parent = null. Typed properties are uninitialized until set — accessing before initialization throws Error. They cannot have default values of incompatible types. Nullable properties must be declared as ?Type and can be null. Static properties can also be typed. This enables: PHPStan/Psalm to track property types through assignments, complete class invariant checking, IDE type inference for all property access. PHP 8.1 adds readonly properties (write-once).

Common Misconception

Typed properties work like typed parameters — accessing an untyped property that was never set returns null. Accessing a typed property before initialization throws Error.

Why It Matters

Typed properties complete PHP's type system at the object level — without them, class state was opaque to static analysis tools.

Common Mistakes

  • Accessing typed properties in constructors before they're initialized.
  • Not declaring nullable for optional properties (?Type).
  • Using mixed type — loses all type safety.

Code Examples

✗ Vulnerable
class User {
    public $name; // Could be anything
    public $age;  // Could be anything
}
✓ Fixed
class User {
    public string $name;
    public int $age;
    public ?string $bio = null;
    public readonly DateTime $createdAt;

    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
        $this->createdAt = new DateTime();
    }
}

Added 23 Mar 2026
Views 114
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
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 4 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 0 pings S 1 ping M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 11 Scrapy 8 Google 7 Ahrefs 7 Perplexity 6 SEMrush 6 Unknown AI 5 Bing 5 PetalBot 5 Twitter/X 3 Meta AI 2 Applebot 2 ChatGPT 1 Brave Search 1
crawler 64 crawler_json 3 your_contextpost 1 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Add type declarations to all class properties. Use ?Type for nullable. Initialize in constructor or use = default for simple types. Run PHPStan level 8+ to detect uninitialized access.
📦 Applies To
PHP 7.4+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
public \$|private \$|protected \$
Auto-detectable: ✓ Yes phpstan psalm rector
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Medium ✓ Auto-fixable Fix: Medium Context: Class


✓ schema.org compliant