myclabs /DeepCopy

Create deep copies (clones) of your objects

★ 8,884⑂ 107👁 21⑃ 126● 83▤ 39
PHPMITCreated 2013-07-22
Download: 1.14.01.14.0 ↓1.14.02026-08-11
+0
Daily
8,884
stars
107
forks
21
watchers
126
prs
83
issues
39
releases

README

DeepCopy

DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph.




 Table of Contents

1. How
1. Why
    1. Using simply clone
    1. Overriding __clone()
    1. With DeepCopy
1. How it works
1. Going further
    1. Matchers
        1. Property name
        1. Specific property
        1. Type
    1. Filters
        1. SetNullFilter
        1. KeepFilter
        1. DoctrineCollectionFilter
        1. DoctrineEmptyCollectionFilter
        1. DoctrineProxyFilter
        1. ReplaceFilter
        1. ShallowCopyFilter
1. Edge cases
1. Contributing
    1. Tests


 How?

Install with Composer:


composer require myclabs/deep-copy


Use it:

php
use DeepCopy\DeepCopy;

$copier = new DeepCopy();
$myCopy = $copier-copy($myObject);



 Why?

- How do you create copies of your objects?

php
$myCopy = clone $myObject;


- How do you create deep copies of your objects (i.e. copying also all the objects referenced in the properties)?

You use __clone() and implement the behavior
yourself.

- But how do you handle cycles in the association graph?

Now you're in for a big mess :(

!association graph


 Using simply clone

!Using clone


 Overriding __clone()

!Overriding __clone


 With DeepCopy

!With DeepCopy


 How it works

DeepCopy recursively traverses all the object's properties and clones them. To avoid cloning the same object twice it
keeps a map of source objects to their copies and thus preserves the object graph.

To use it:

php
use function DeepCopy\deep_copy;

$copy = deep_copy($var);


Alternatively, you can create your own DeepCopy instance to configure it differently for example:

php
use DeepCopy\DeepCopy;

$copier = new DeepCopy(true);

$copy = $copier-copy($var);


You may want to roll your own deep copy function:

php
namespace Acme;

use DeepCopy\DeepCopy;

function deep_copy($var)
{
    static $copier = null;
    
    if (null === $copier) {
        $copier = new DeepCopy(true);
    }
    
    return $copier-copy($var);
}



 Going further

You can add filters to customize the copy process.

The method to add a filter is DeepCopy\DeepCopy::addFilter($filter, $matcher),
with $filter implementing DeepCopy\Filter\Filter
and $matcher implementing DeepCopy\Matcher\Matcher.

We provide some generic filters and matchers.


 Matchers

  - DeepCopy\Matcher applies on a object attribute.
  - DeepCopy\TypeMatcher applies on any element found in graph, including array elements.


 Property name

The PropertyNameMatcher will match a property by its name:

php
use DeepCopy\Matcher\PropertyNameMatcher;

// Will apply a filter to any property of any objects named "id"
$matcher = new PropertyNameMatcher('id');



 Specific property

The PropertyMatcher will match a specific property of a specific class:

php
use DeepCopy\Matcher\PropertyMatcher;

// Will apply a filter to the property "id" of any objects of the class "MyClass"
$matcher = new PropertyMatcher('MyClass', 'id');



 Type

The

Top issues

Latest release

1.14.0 1.14.0 2026-08-11
What's Changed

PHP 8.6 is supported and PHP 7.x support has been dropped (8.0 or greater is required now).

 Run tests on PHP 8.5 by @Chris53897 in https://github.com/myclabs/DeepCopy/pull/208
 Replace spl_object_hash with spl_object_id by @mnapoli in https://github.com/myclabs/DeepCopy/pull/210
 Switch to WeakMap by @mnapoli in https://github.com/myclabs/DeepCopy/pull/211


Full Changelog: https://github.com/myclabs/DeepCopy/compare/1.13.4...1.14.0

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON