{
    "slug": "intersection_types",
    "term": "Intersection Types (PHP 8.1)",
    "category": "php",
    "difficulty": "intermediate",
    "short": "Require a value to satisfy multiple type constraints simultaneously, declared as TypeA&TypeB — useful for combining interfaces.",
    "long": "Intersection types (PHP 8.1) declare that a value must implement all of the listed types. For example, function process(Countable&Iterator $collection) requires the argument to implement both interfaces. This is especially useful in generic-style code that needs an object to fulfil multiple contracts without creating a new combined interface. Intersection types only work with class/interface types, not with scalar types, and cannot currently be combined with union types in the same declaration (PHP 8.1).",
    "aliases": [
        "PHP intersection types",
        "PHP 8.1 intersection",
        "combined interface types"
    ],
    "tags": [
        "php8",
        "php",
        "type-system"
    ],
    "misconception": "Intersection types and union types are two ways to express the same thing. Union types mean \"one of these types\"; intersection types mean \"all of these types simultaneously\" — useful for requiring an argument to implement multiple interfaces at once.",
    "why_it_matters": "PHP 8.1 intersection types (A&B) require a value to satisfy multiple type constraints simultaneously — useful for parameters that must implement several interfaces at once.",
    "common_mistakes": [
        "Using intersection types where a single interface combining both contracts would be cleaner.",
        "Combining non-interface types in an intersection — PHP only allows interfaces and classes, not scalar types.",
        "Not realising intersection types cannot be used with union types in the same declaration in PHP 8.1 — use PHP 8.2 DNF types (A&B)|null for that.",
        "Overusing intersection types making signatures hard to read — extract a named interface when the combination recurs."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "union_types",
        "interfaces",
        "strict_types"
    ],
    "prerequisites": [
        "type_declarations",
        "interfaces",
        "phpstan_levels"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.intersection"
    ],
    "bad_code": "// PHP 8.1 — cannot combine intersection and union:\nfunction process(Countable&Iterator|null $input): void {} // Error in PHP 8.1\n\n// PHP 8.2 DNF types solve this:\nfunction process((Countable&Iterator)|null $input): void {} // Valid in PHP 8.2+",
    "good_code": "// PHP 8.1 — value must satisfy ALL listed types simultaneously\nfunction process(Iterator&Countable \\$collection): void {\n    echo count(\\$collection);      // Countable\n    foreach (\\$collection as \\$item) { handle(\\$item); } // Iterator\n}\n\n// PHP 8.2 DNF types — mix union + intersection:\nfunction accept((Iterator&Countable)|array \\$items): void {}\n\n// Practical: enforce a collection interface\ninterface Collection extends Countable, Iterator {}\n\nfunction paginate(Countable&Iterator \\$items, int \\$perPage = 20): array {\n    return ['total' => count(\\$items), 'items' => iterator_to_array(\\$items)];\n}",
    "quick_fix": "Use intersection types (Countable&Iterator) in PHP 8.1 to express that a parameter must implement multiple interfaces — previously required a custom interface extending both",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/intersection_types",
        "html_url": "https://codeclaritylab.com/glossary/intersection_types",
        "json_url": "https://codeclaritylab.com/glossary/intersection_types.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 Types (PHP 8.1)](https://codeclaritylab.com/glossary/intersection_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/intersection_types"
            }
        }
    }
}