{
    "slug": "php_type_error",
    "term": "TypeError — When Type Declarations Throw",
    "category": "php",
    "difficulty": "intermediate",
    "short": "TypeError is thrown when a value passed to a typed parameter or return type doesn't match — strict_types=1 makes this happen at call sites too.",
    "long": "In PHP 7+, type declarations throw TypeError when violated. With declare(strict_types=1), PHP enforces types strictly at call sites. Without it, PHP coerces where possible (int '5' → 5). TypeError extends Error (not Exception) — catch it separately or use Throwable. Common triggers: passing string to int parameter with strict_types, returning wrong type, violating union types. PHP 8.1 intersection types and DNF types (PHP 8.2) add more ways to throw TypeError.",
    "aliases": [],
    "tags": [
        "php",
        "types",
        "errors",
        "strict-types"
    ],
    "misconception": "TypeError only happens with strict_types — without it PHP silently coerces, but some coercions are still impossible (object to int) and still throw TypeError.",
    "why_it_matters": "TypeErrors surface type contract violations at the boundary rather than deep inside a function, making bugs easier to locate and fix.",
    "common_mistakes": [
        "Catching Exception instead of Error or Throwable for TypeError.",
        "Not using strict_types=1 — coercion hides bugs.",
        "Returning null from a non-nullable return type — use ?Type or add null to union."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_exception_hierarchy",
        "strict_types",
        "type_declarations",
        "type_coercion"
    ],
    "prerequisites": [
        "strict_types",
        "type_declarations"
    ],
    "refs": [
        "https://www.php.net/manual/en/class.typeerror.php"
    ],
    "bad_code": "function add(int $a, int $b): int { return $a + $b; }\nadd('five', 2); // Without strict_types: coercion\n// With strict_types=1: TypeError: add(): Argument #1 must be int, string given",
    "good_code": "declare(strict_types=1);\nfunction add(int $a, int $b): int { return $a + $b; }\n\ntry {\n    add('five', 2);\n} catch (\\TypeError $e) {\n    throw new \\InvalidArgumentException('Numeric values required', 0, $e);\n}",
    "quick_fix": "Use declare(strict_types=1) at top of file and catch \\TypeError separately from \\Exception — or catch \\Throwable for both.",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_type_error",
        "html_url": "https://codeclaritylab.com/glossary/php_type_error",
        "json_url": "https://codeclaritylab.com/glossary/php_type_error.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": "[TypeError — When Type Declarations Throw](https://codeclaritylab.com/glossary/php_type_error) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_type_error"
            }
        }
    }
}