{
    "slug": "php8",
    "term": "PHP 8 — Key Features",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP 8.0–8.4 introduced match expressions, named arguments, union types, nullsafe operator, fibers, enums, readonly properties, first-class callables, and a JIT compiler — the most significant evolution of the language since PHP 5.",
    "long": "PHP 8.0 (2020): named arguments, union types, match expression (strict, no fall-through, returns a value), nullsafe operator (?->), JIT compiler, constructor property promotion, str_contains/str_starts_with/str_ends_with, throw as expression, attributes. PHP 8.1 (2021): enums, readonly properties, fibers, intersection types, never return type, array_is_list(), first-class callable syntax (strlen(...)), new in initializers. PHP 8.2 (2022): readonly classes, disjunctive normal form (DNF) types, deprecated dynamic properties, true/false/null standalone types. PHP 8.3 (2023): typed class constants, json_validate(), Override attribute, deep-cloning of readonly properties. PHP 8.4 (2024): property hooks, asymmetric visibility (public get / protected set), updated array functions (array_find, array_any, array_all), HTML5 parser for DOM extension.",
    "aliases": [
        "PHP 8",
        "PHP8",
        "PHP 8.0",
        "PHP 8.1",
        "PHP 8.2",
        "PHP 8.3",
        "PHP 8.4",
        "modern PHP"
    ],
    "tags": [
        "php",
        "php8",
        "modern-php",
        "named-arguments",
        "match",
        "enums",
        "fibers",
        "readonly"
    ],
    "misconception": "PHP 8's JIT compiler makes all PHP code significantly faster. The JIT primarily benefits CPU-bound code like mathematical computations, image processing, and scientific calculations. Typical web applications are I/O-bound — most time is spent waiting for database queries, HTTP calls, and file reads — and see negligible JIT speedup. The more impactful PHP 8 performance improvement is OPcache improvements and the overall engine optimisations, not the JIT itself.",
    "why_it_matters": "PHP 8 is not a minor version bump — it introduces language features that fundamentally change how clean PHP code is written. Match expressions eliminate fall-through bugs from switch. Named arguments make function calls self-documenting and allow skipping optional parameters. Constructor promotion halves the boilerplate for value objects. Enums replace fragile string/integer constants with type-safe objects. Fibers enable async frameworks without callbacks. PHP 7 codebases miss all of these, and the upgrade path is well-documented with automated migration tooling (Rector).",
    "common_mistakes": [
        "Using match without covering all cases — match throws UnhandledMatchError if no arm matches and there is no default; always include a default arm or ensure exhaustive coverage.",
        "Confusing readonly properties with immutable objects — readonly prevents reassignment after initialisation but does not deep-clone objects; a readonly property holding an object can still have its object's properties mutated.",
        "Using dynamic properties removed in PHP 8.2 — assigning to undeclared properties throws a deprecation in 8.1 and an error in 8.2; declare all properties explicitly.",
        "Expecting JIT to speed up database-heavy web requests — benchmark before enabling JIT; the overhead of JIT compilation can slow simple scripts."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_migrate_7_to_8",
        "php_property_hooks_82",
        "php_asymmetric_visibility_84",
        "fibers"
    ],
    "prerequisites": [],
    "refs": [
        "https://www.php.net/releases/8.0/",
        "https://www.php.net/releases/8.4/"
    ],
    "bad_code": "// PHP 7 style — verbose, error-prone\nfunction createUser(string $name, ?string $email = null, int $role = 0): User {\n    $this->name  = $name;\n    $this->email = $email;\n    $this->role  = $role;\n}\n\n$status = 'active';\n$label = $status === 'active' ? 'Active' : ($status === 'pending' ? 'Pending' : 'Unknown');",
    "good_code": "// PHP 8 style — concise, type-safe\nenum Status { case Active; case Pending; case Suspended; }\n\nclass User {\n    public function __construct(\n        public readonly string  $name,\n        public readonly ?string $email = null,\n        public readonly Status  $status = Status::Active,\n    ) {}\n}\n\n$label = match($status) {\n    Status::Active  => 'Active',\n    Status::Pending => 'Pending',\n    default         => 'Unknown',\n};",
    "quick_fix": "Run Rector with the PHP80/PHP81/PHP82 sets to automatically migrate PHP 7 code to modern PHP 8 equivalents — handles most common patterns automatically",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-04-04",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php8",
        "html_url": "https://codeclaritylab.com/glossary/php8",
        "json_url": "https://codeclaritylab.com/glossary/php8.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": "[PHP 8 — Key Features](https://codeclaritylab.com/glossary/php8) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php8"
            }
        }
    }
}