DNF Types (PHP 8.2)
TL;DR
PHP 8.2 DNF (Disjunctive Normal Form) types allow unions of intersections — (Countable&Iterator)|null — combining PHP 8.1 intersection types with union types.
Explanation
DNF type: (A&B)|C where intersections appear in parentheses within a union. PHP 8.1 added intersection types (A&B — object must implement both). PHP 8.2 adds DNF — combining intersections with unions: (Countable&Traversable)|null means either null OR an object implementing both Countable and Traversable. Rules: intersections must be in parentheses within unions. Cannot have redundant types. Cannot duplicate types within a union. Use cases: nullable intersection types (the most common: (A&B)|null), optional complex type parameters, return types that are either an intersection or a primitive.
Common Misconception
✗ (A&B)|C means A and B or C — it means (A&&B)||C. An object satisfying both A and B, OR the value C.
Why It Matters
DNF types complete PHP's type system by allowing nullable intersection types — the most common real-world case (an object implementing multiple interfaces, or null).
Common Mistakes
- Forgetting parentheses around intersection in union — syntax error.
- Redundant types in union — (A&A)|B is invalid.
- Using DNF where simple union suffices — only needed for intersections in unions.
Code Examples
✗ Vulnerable
// PHP 8.1 — no way to express nullable intersection:
function process(Countable&Iterator $items): void {} // Non-nullable only
✓ Fixed
// PHP 8.2 DNF — nullable intersection:
function process((Countable&Iterator)|null $items): void {
if ($items === null) return;
// $items is guaranteed to be both Countable AND Iterator
}
// Other uses:
function transform((Stringable&JsonSerializable)|array $data): string {}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
22
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 7
Amazonbot 5
Unknown AI 3
Google 2
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 17
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Low
⚡ Quick Fix
Use (A&B)|null for nullable intersection types. Always put intersections in parentheses within unions. Only supported from PHP 8.2+.
📦 Applies To
PHP 8.2+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
[A-Z][a-z]+&[A-Z].*\|
Auto-detectable:
✓ Yes
phpstan
psalm
🤖 AI Agent
Confidence: Low
False Positives: Medium
✗ Manual fix
Fix: Low
Context: Function