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

Intersection & DNF Types in Practice

PHP PHP 8.1+ Advanced
debt(d1/e1/b3/t5)
d1 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'caught instantly' (d1). Misusing intersection types with scalars (int&string) causes a fatal parse error at compile time. PHPStan and Psalm (from detection_hints.tools) also catch type mismatches statically, but the PHP parser itself catches the most common mistake immediately.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch' (e1). Per quick_fix, the fix is straightforward syntax adjustment: use A&B for objects, add parentheses for DNF (A&B)|null. These are single-line corrections to type declarations, not architectural changes.

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

Closest to 'localised tax' (b3). Intersection types are opt-in type declarations that affect only the specific function/method signatures where used. They don't impose system-wide constraints. However, once adopted in a codebase's interfaces, they do become part of the API contract that dependents must satisfy, creating some localized commitment.

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

Closest to 'notable trap' (t5). The misconception explicitly states developers assume intersection works with scalars like int&string when it only works with object types. Additionally, confusing union (A|B = either) with intersection (A&B = both) is a documented common mistake. These are gotchas that PHP developers eventually learn but aren't obvious from other language experience.

About DEBT scoring →

TL;DR

PHP 8.1 intersection types (A&B) and PHP 8.2 DNF types ((A&B)|null) allow precise type constraints for objects implementing multiple interfaces.

Explanation

Intersection types (PHP 8.1): function process(Countable&Iterator $c) — $c must implement BOTH interfaces. Only works with class/interface types, not scalars. DNF (Disjunctive Normal Form) types (PHP 8.2): (Countable&Iterator)|null — unions of intersections. The DNF form requires intersections in parentheses inside unions. Use cases: utility functions that require an object to implement multiple interfaces, nullable intersection types. PHPStan and Psalm both support intersection types for static analysis.

Common Misconception

Intersection types work with scalar types like int&string — they only work with object types (classes and interfaces).

Why It Matters

Intersection types enable precise type constraints that were previously impossible — expressing 'must implement both A and B' without creating a new combined interface.

Common Mistakes

  • Using intersection with scalar types — fatal error.
  • Not using DNF parentheses for (A&B)|null — required syntax in PHP 8.2.
  • Confusing union (A|B = either) with intersection (A&B = both).

Code Examples

✗ Vulnerable
// Can't use intersection with scalars:
function foo(int&string $x) {} // Fatal Error
✓ Fixed
interface Serializable {}
interface Loggable {}

// Must implement BOTH:
function process(Serializable&Loggable $obj): void {}

// PHP 8.2 DNF — nullable intersection:
function maybe((Serializable&Loggable)|null $obj): void {}

Added 23 Mar 2026
Views 41
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 2 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 2 pings F 0 pings S 0 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
No pings yet today
No pings yesterday
Amazonbot 7 Google 7 Perplexity 5 Unknown AI 3 Ahrefs 3 SEMrush 3 Claude 2 PetalBot 2 ChatGPT 1 Meta AI 1 Scrapy 1
crawler 28 crawler_json 5 pre-tracking 2
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Medium
⚡ Quick Fix
Use A&B intersection for objects required to implement multiple interfaces. Use (A&B)|null for nullable with DNF syntax. Scalars not supported.
📦 Applies To
PHP 8.1+ 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


✓ schema.org compliant