explaingit

myclabs/deepcopy

8,896PHPAudience · developerComplexity · 2/5Setup · easy

TLDR

A PHP library that creates fully independent copies of complex objects, including ones with circular references, so modifying the copy never accidentally changes the original.

Mindmap

mindmap
  root((deepcopy))
    What it does
      Deep copy PHP objects
      Handles circular refs
    How it works
      Filters and matchers
      Recursive traversal
    Integrations
      Doctrine ORM
      PHPUnit testing
    Audience
      PHP developers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Duplicate a PHP database entity object so you can save it as a new record without affecting the original.

USE CASE 2

Clone a Doctrine ORM entity graph while automatically nulling out the ID so a new database row gets created.

USE CASE 3

Copy a complex nested object tree in tests so modifications in one test case don't bleed into others.

Tech stack

PHPComposer

Getting it running

Difficulty · easy Time to first run · 5min

Install via Composer with a single command, no external infrastructure or services needed.

In plain English

DeepCopy is a PHP library that creates full, independent copies of objects, including all the objects they reference internally. In programming, when you copy an object with PHP's built-in clone command, only the top-level object is duplicated. Any nested objects it points to still refer to the same underlying data, which can cause unexpected behavior when you modify the copy. DeepCopy solves this by walking through all of an object's properties recursively and duplicating each one. A particular challenge it addresses is circular references, where object A references object B, which in turn references object A. Naive deep copy approaches can get stuck in an infinite loop or create duplicate copies of the same object. DeepCopy tracks which objects have already been cloned and reuses those copies, preserving the original structure without getting caught in loops. The library also allows you to customize the copy process using filters and matchers. A matcher identifies which properties or types to target, and a filter describes what to do with them. For example, you can tell DeepCopy to set a property named "id" to null in the copy (useful when duplicating a database record that should get a new identifier), or to leave a particular property untouched rather than copying it. Several built-in filters handle common cases when working with Doctrine, a popular PHP database library. Installation is done through Composer, the standard PHP package manager, with a single command. The basic usage is straightforward: create a DeepCopy instance and call its copy method with the object you want to duplicate. The library is installed automatically when you use PHPUnit, a widely used PHP testing framework, which partly explains its high download count.

Copy-paste prompts

Prompt 1
Using myclabs/deepcopy, show me how to deep-copy a Doctrine entity and set its 'id' property to null so I can persist it as a new record.
Prompt 2
I have a PHP object with circular references between two classes. Write code using DeepCopy to safely clone it without hitting an infinite loop.
Prompt 3
How do I add a custom filter to myclabs/deepcopy so that a specific property is left untouched during the copy operation?
Prompt 4
Generate the Composer install command and a minimal usage example for deepcopy that duplicates a nested PHP object and verifies the copy is independent.
Open on GitHub → Explain another repo

← myclabs on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.