{
    "slug": "php_intersection_dnf",
    "term": "Intersection & DNF Types in Practice",
    "category": "php",
    "difficulty": "advanced",
    "short": "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.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "php",
        "types",
        "php81",
        "php82",
        "intersection"
    ],
    "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)."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "union_types",
        "intersection_types",
        "interfaces",
        "type_declarations"
    ],
    "prerequisites": [
        "union_types",
        "interfaces"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.types.type-system.php"
    ],
    "bad_code": "// Can't use intersection with scalars:\nfunction foo(int&string $x) {} // Fatal Error",
    "good_code": "interface Serializable {}\ninterface Loggable {}\n\n// Must implement BOTH:\nfunction process(Serializable&Loggable $obj): void {}\n\n// PHP 8.2 DNF — nullable intersection:\nfunction maybe((Serializable&Loggable)|null $obj): void {}",
    "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.",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_intersection_dnf",
        "html_url": "https://codeclaritylab.com/glossary/php_intersection_dnf",
        "json_url": "https://codeclaritylab.com/glossary/php_intersection_dnf.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Intersection & DNF Types in Practice](https://codeclaritylab.com/glossary/php_intersection_dnf) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_intersection_dnf"
            }
        }
    }
}