Typed Properties (PHP 7.4)
debt(d5/e3/b3/t7)
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.
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.
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.
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).
TL;DR
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
Why It Matters
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
class User {
public $name; // Could be anything
public $age; // Could be anything
}
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();
}
}