← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Prototype Pattern

Code Quality PHP 5.0+ Intermediate
debt(d7/e3/b3/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7), shared-reference bugs from shallow clone don't trigger errors; PHPStan won't flag missing __clone() implementations, surfaces via behavioural testing.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3), quick_fix is implementing __clone() on affected classes to deep-copy nested objects — localized change per class but may need it on several types.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3), the pattern applies where object templates are needed; it's a per-class implementation choice, doesn't shape the whole system.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7), the misconception explicitly states devs assume clone is deep but it's shallow — contradicts the intuitive meaning of 'clone' and silently shares state across instances.

About DEBT scoring →

Also Known As

clone pattern object cloning PHP clone

TL;DR

Creating new objects by cloning a prototype — fast when object creation is expensive and variations are needed.

Explanation

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.

Common 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

Code Examples

✗ Vulnerable
$clone = clone $template;
$clone->items->add($extra); // Also modifies $template! Shared reference
✓ Fixed
class Order {
    public function __clone(): void { $this->items = clone $this->items; }
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 ChatGPT 4 Perplexity 3 Google 3 Ahrefs 3 Unknown AI 2 Claude 2 SEMrush 2 Scrapy 2 Bing 1
crawler 24 crawler_json 5
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ 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
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Manually recreating complex objects when clone would copy them; shared state bug from shallow copy of object with nested objects
Auto-detectable: ✗ No phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update


✓ schema.org compliant