{
    "slug": "nullable_types",
    "term": "Nullable Types (?Type)",
    "category": "php",
    "difficulty": "beginner",
    "short": "Declaring a type as ?Type allows either a value of that type or null — shorthand for Type|null.",
    "long": "Nullable types (PHP 7.1+) prefix a type declaration with ? to indicate the value may be null in addition to the declared type. function find(?int $id): ?User accepts null for $id and may return a User or null. This is equivalent to the union type int|null and User|null introduced in PHP 8.0. Nullable types made it practical to type-hint optional parameters and nullable return values without resorting to mixed. Combine with the null coalescing operator (??) and nullsafe operator (?->) for clean null handling.",
    "aliases": [
        "nullable type",
        "?string",
        "PHP nullable"
    ],
    "tags": [
        "php7",
        "php",
        "type-system",
        "null-safety"
    ],
    "misconception": "Nullable types encourage passing null everywhere. They do the opposite — they make nullability explicit and opt-in. A function typed string cannot receive null; only ?string can. This forces developers to handle null intentionally rather than accidentally.",
    "why_it_matters": "PHP's nullable type syntax (?Type) makes null a documented, type-checked part of the contract — without it, null silently passes through typed parameters causing downstream failures.",
    "common_mistakes": [
        "Using ?Type when null should actually be forbidden — nullable types are sometimes overused to avoid validation.",
        "Forgetting that nullable return types require explicit return null statements — returning nothing returns null implicitly but ?Type requires it to be intentional.",
        "Not using the nullsafe operator (?->) when chaining calls on nullable return values.",
        "Returning null from constructors — constructors cannot be nullable; throw an exception instead."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "null_coalescing",
        "nullsafe_operator",
        "union_types",
        "strict_types"
    ],
    "prerequisites": [
        "type_declarations",
        "null_coalescing",
        "nullsafe_operator"
    ],
    "refs": [
        "https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.nullable-types"
    ],
    "bad_code": "// Nullable parameter used where null should be invalid:\nfunction setAge(?int $age): void {\n    $this->age = $age; // Allows null — but a person must have an age\n}\n\n// If null means 'unknown', model it explicitly:\nfunction setAge(int $age): void { // Enforce valid age\n    if ($age < 0 || $age > 150) throw new InvalidArgumentException();\n    $this->age = $age;\n}",
    "good_code": "// ?Type is shorthand for Type|null\nfunction findUser(int \\$id): ?User {\n    return \\$this->db->find(\\$id); // null if not found\n}\n\n// Nullable parameter with default\nfunction greet(?string \\$name = null): string {\n    return 'Hello, ' . (\\$name ?? 'Guest');\n}\n\n// Typed nullable property (PHP 7.4+)\nclass Order {\n    public ?\\DateTimeImmutable \\$shippedAt = null; // not shipped yet\n}\n\n// PHP 8.0+ — use union type for explicit null\nfunction process(int|null \\$value): void {}\n// Equivalent to: function process(?int \\$value): void {}",
    "quick_fix": "Use ?string for optional parameters and return types — but consider whether null is the right signal; sometimes an empty collection, a NullObject, or an Optional-style type communicates intent better",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/nullable_types",
        "html_url": "https://codeclaritylab.com/glossary/nullable_types",
        "json_url": "https://codeclaritylab.com/glossary/nullable_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": "[Nullable Types (?Type)](https://codeclaritylab.com/glossary/nullable_types) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/nullable_types"
            }
        }
    }
}