Type Declarations Overview
Also Known As
PHP type hints
PHP return types
typed properties
TL;DR
PHP's full type system: scalar types, return types, union types, intersection types, nullable, never, and mixed — all enforced at runtime.
Explanation
PHP's type system has grown substantially: PHP 7.0 added scalar type hints (int, float, string, bool) and return types; 7.1 added nullable (?Type) and void; 7.4 added typed properties; 8.0 added union types (A|B), mixed, static return type, and the never placeholder; 8.1 added intersection types (A&B), never return type, and readonly properties; 8.2 added standalone null, false, true types. Combine with declare(strict_types=1) for strict runtime enforcement. Static analysers (PHPStan, Psalm) use these to verify type correctness without execution.
Common Misconception
✗ Type declarations in PHP are just documentation hints. In PHP 7+ they are enforced at runtime — passing the wrong type throws a TypeError. With strict_types=1 they become strict; without it PHP coerces compatible types. Typed properties (PHP 7.4+) additionally prevent uninitialized access.
Why It Matters
Type declarations turn runtime surprises into parse-time or call-time errors — a function that declares int $id cannot silently receive a user-supplied string. They are the lowest-effort, highest-return safety net in PHP.
Common Mistakes
- Not enabling strict_types=1 — without it PHP coerces types silently, defeating much of the safety benefit.
- Using mixed or no type at all "just in case" — be as specific as possible, loosen only when truly needed.
- Declaring nullable types (?string) out of habit when null is never a valid input.
- Forgetting return type declarations — parameter types alone leave the output unguarded.
Avoid When
- Avoid mixed as a type declaration — it disables type checking entirely and signals unclear design.
- Do not use type declarations as a substitute for input validation on data coming from outside PHP.
When To Use
- Declare parameter and return types on all public methods — they serve as enforced documentation.
- Use declare(strict_types=1) at the top of every PHP file to prevent silent type coercion.
Code Examples
✗ Vulnerable
// No type declarations — bugs hide until runtime:
function applyDiscount($price, $percent) {
return $price - ($price * $percent / 100);
}
applyDiscount('ten dollars', '20%'); // No error — returns 0
// With type declarations:
function applyDiscount(float $price, float $percent): float {
return $price - ($price * $percent / 100);
}
applyDiscount('ten dollars', '20%'); // TypeError immediately
✓ Fixed
<?php declare(strict_types=1);
class UserService {
// Typed property (PHP 7.4)
private array $cache = [];
// Parameter + return types
public function findById(int $id): ?User { return null; }
// Union type (PHP 8.0)
public function findByIdOrEmail(int|string $identifier): ?User { return null; }
// Intersection type (PHP 8.1) — must be Countable AND Iterator
public function processAll(\Countable&\Iterator $items): void {}
// never return type (PHP 8.1)
public function fail(string $msg): never {
throw new \RuntimeException($msg);
}
// Enum param (PHP 8.1)
public function filterByStatus(OrderStatus $status): array { return []; }
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
31 Mar 2026
Views
80
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
ChatGPT 45
Perplexity 10
Amazonbot 8
Unknown AI 3
SEMrush 3
Google 2
Ahrefs 2
Also referenced
How they use it
crawler 71
crawler_json 2
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Medium
⚡ Quick Fix
Add declare(strict_types=1) at the top of every file and add parameter/return types to every function signature
📦 Applies To
PHP 7.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Function declarations missing parameter types or return types; no declare(strict_types=1)
Auto-detectable:
✓ Yes
phpstan
psalm
phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Medium
False Positives: Low
✓ Auto-fixable
Fix: Low
Context: Function