The Bug Everyone Missed: A Lesson in Assumptions, Cloning, and Mastery
Several senior engineers and two code reviews missed it: a bug hiding in how PHP clones objects.

First published on LinkedIn on 16 January 2026. This copy is the one I keep up to date.
Multiple senior engineers. Two code reviews. One deploy to production. And somehow, the bug slipped through all of it.
Recently, a colleague ran into a problem that many experienced developers will quietly nod along to: the kind of issue that hides in plain sight.
The task sounded straightforward:
Create a new version of a main record by cloning an existing one. Each version should stand on its own, be clean, with its own ID, and be independent.
But something strange happened.
Every new version kept inheriting all the child records from previous versions. Not just the original ones - everything. Over time, versions became bloated with data that clearly didn’t belong there.
Multiple experienced eyes reviewed the code. Nothing looked obviously wrong. And yet, the bug persisted.
The subtle culprit
The issue came down to a quiet assumption:
When I clone something, I get a fresh copy.
In PHP, cloning an object does not mean copying everything deeply. When an object contains other objects - like the Collection objects that Doctrine uses to manage related records - PHP copies only the reference to those objects, not the objects themselves. This is called a shallow copy.
So while the parent record looked new, it was secretly still connected to the exact same collection of children as the original.
Here’s what that meant in practice: when the system later added child records to what it thought was the new version’s collection, it was actually modifying the one shared collection that both versions pointed to. Every version accumulated every child, because behind the scenes, there was only ever one collection being passed around. ā
PHP did exactly what it was designed to do. And the bug lived quietly in that gap between expectation and reality.
The fix (and the mindset shift)
The immediate fix was simple once understood:
Reset the child collections inside the object’s __clone() method. This magic method runs automatically whenever an object is cloned, giving you a chance to break those shared references and create fresh, empty collections for the new instance.
After the fix, the same scenario played out correctly:
Create Version 1 with children A and B
Clone to create Version 2 (now starts with an empty collection)
Add Child C to Version 2
Result:
Version 1: 2 children (A, B) ā
Version 2: 1 child (C) ā
Each version now maintained its own independent collection: problem solved.
But what made this moment valuable wasn’t just the fix - it was the realisation.
A future-proof way forward
Fixing the issue exposed a broader opportunity: how do we make sure this problem never quietly returns?
A more robust approach is to introduce a reusable cloning service - one that understands the entity’s structure and performs a true deep clone automatically. Instead of relying on developers to remember which properties or relationships need special handling, the service can inspect the entity and recreate each part in a safe, predictable way.
The real strength of this solution is future-proofing.
As new properties or relationships are added to the entity, they are automatically handled by the cloning process. No hidden coupling. No fragile assumptions. The code adapts naturally as the domain evolves.
It’s the difference between fixing today’s issue and preventing an entire class of tomorrow’s bugs.
The bigger takeaway
This wasn’t really about PHP or any specific framework.
It was about assumptions.
Even mature, well-designed features can behave differently than we intuitively expect. And the more experienced we become, the more dangerous “I already know how this works” can be.
This story is a reminder that:
Bugs don’t always live in complex logic - sometimes they live in defaults.
Experience doesn’t make us immune to blind spots.
Great engineering isn’t just about writing code, but about understanding our tools deeply enough to know where they won’t behave as we expect.
And perhaps most importantly:
The best teams don’t just fix bugs - they learn from them.
If you’ve ever stared at a problem thinking “this should work”, only to later discover a tiny assumption was steering you wrong - you’re in good company.
That’s not failure.
That’s mastery in progress, the old fashioned way.
Shout-out to JosĆ© Neves and Team Portu! šµš¹