{
    "slug": "prototype_pattern_creational",
    "term": "Prototype Pattern",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "Creating new objects by cloning a prototype — fast when object creation is expensive and variations are needed.",
    "long": "PHP clone creates a shallow copy; __clone() handles deep-copy logic for nested objects. Use cases: expensive-to-create objects, creating variations of a base object, undo/redo snapshots. Without __clone(), nested objects share references between clones.",
    "aliases": [
        "clone pattern",
        "object cloning",
        "PHP clone"
    ],
    "tags": [
        "patterns",
        "oop",
        "php"
    ],
    "misconception": "PHP clone always creates a deep copy — clone is shallow; nested objects share references unless __clone() explicitly clones them.",
    "why_it_matters": "Without Prototype, creating 1000 order templates requires 1000 expensive constructor calls — cloning a template is fast.",
    "common_mistakes": [
        "Assuming clone deep-copies",
        "Not implementing __clone() for nested objects",
        "State leaking via shared references"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "memento_pattern",
        "builder_pattern"
    ],
    "prerequisites": [
        "factory_pattern",
        "interfaces",
        "memory_management"
    ],
    "refs": [
        "https://refactoring.guru/design-patterns/prototype"
    ],
    "bad_code": "$clone = clone $template;\n$clone->items->add($extra); // Also modifies $template! Shared reference",
    "good_code": "class Order {\n    public function __clone(): void { $this->items = clone $this->items; }\n}",
    "quick_fix": "Use PHP's clone keyword to implement the Prototype pattern — deep clone complex objects with clone and override __clone() to deep-copy nested objects that would otherwise share references",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/prototype_pattern_creational",
        "html_url": "https://codeclaritylab.com/glossary/prototype_pattern_creational",
        "json_url": "https://codeclaritylab.com/glossary/prototype_pattern_creational.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": "[Prototype Pattern](https://codeclaritylab.com/glossary/prototype_pattern_creational) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/prototype_pattern_creational"
            }
        }
    }
}