{
    "slug": "php8_readonly_intro",
    "term": "readonly Properties (PHP 8.1)",
    "category": "php",
    "difficulty": "beginner",
    "short": "PHP 8.1 readonly properties can only be written once (in the constructor) — enforcing immutability without verbose accessor boilerplate.",
    "long": "public readonly string $name; cannot be written after initialization. Writing a second time throws Error. Cannot be unset. Must be typed. Cannot have a default value in declaration (except via constructor promotion). Combined with constructor promotion: public function __construct(public readonly string $name) — declare + promote + make readonly in one line. PHP 8.2 readonly classes: mark the entire class and all promoted properties are readonly. PHP 8.3 allows readonly property modification in __clone() for clone-with patterns. Use readonly for value objects, DTOs, request objects.",
    "aliases": [],
    "tags": [
        "php",
        "php81",
        "readonly",
        "immutability",
        "history"
    ],
    "misconception": "readonly prevents cloning with modifications — PHP 8.3 allows modifying readonly properties in __clone(). Before 8.3, use a with() method that returns a new instance.",
    "why_it_matters": "readonly eliminates the need for private property + public getter for immutable data — reducing boilerplate and enforcing value object immutability by the language.",
    "common_mistakes": [
        "Trying to set readonly property after construction — throws Error.",
        "Forgetting readonly requires a type declaration.",
        "Not using readonly class (8.2) when all properties should be immutable."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_readonly_promotion",
        "readonly_classes",
        "value_object",
        "php_object_cloning_security"
    ],
    "prerequisites": [
        "php7_typed_properties",
        "php5_oop_model"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties"
    ],
    "bad_code": "class Money {\n    private int $amount;\n    private string $currency;\n\n    public function getAmount(): int { return $this->amount; }\n    public function getCurrency(): string { return $this->currency; }\n}",
    "good_code": "// PHP 8.1 — readonly + constructor promotion:\nclass Money {\n    public function __construct(\n        public readonly int $amount,\n        public readonly string $currency,\n    ) {}\n}\n\n// PHP 8.2 — readonly class:\nreadonly class Money {\n    public function __construct(\n        public int $amount,\n        public string $currency,\n    ) {}\n}",
    "quick_fix": "Add readonly to immutable properties. Combine with constructor promotion for concise value objects. Use readonly class (PHP 8.2) for fully immutable types.",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php8_readonly_intro",
        "html_url": "https://codeclaritylab.com/glossary/php8_readonly_intro",
        "json_url": "https://codeclaritylab.com/glossary/php8_readonly_intro.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": "[readonly Properties (PHP 8.1)](https://codeclaritylab.com/glossary/php8_readonly_intro) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php8_readonly_intro"
            }
        }
    }
}