Download the PHP package myclabs/deep-copy without Composer
On this page you can find all versions of the php package myclabs/deep-copy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
More information about myclabs/deep-copy
Files in myclabs/deep-copy
Informations about the package deep-copy
DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph.
Table of Contents
- Using simply
- Using simply
- How it works
- Going further
- Property name
- Specific property
- Edge cases
Install with Composer:
How do you create copies of your objects?
- How do you create deep copies of your objects (i.e. copying also all the objects referenced in the properties)?
__clone() and implement the behavior
- But how do you handle cycles in the association graph?
Now you're in for a big mess :(
How it works
DeepCopy recursively traverses all the object's properties and clones them. To avoid cloning the same object twice it keeps a hash map of all instances and thus preserves the object graph.
To use it:
Alternatively, you can create your own
DeepCopy instance to configure it differently for example:
You may want to roll your own deep copy function:
You can add filters to customize the copy process.
The method to add a filter is
We provide some generic filters and matchers.
DeepCopy\Matcherapplies on a object attribute.
DeepCopy\TypeMatcherapplies on any element found in graph, including array elements.
PropertyNameMatcher will match a property by its name:
PropertyMatcher will match a specific property of a specific class:
TypeMatcher will match any element by its type (instance of a class or any value that could be parameter of
DeepCopy\Filterapplies a transformation to the object attribute matched by
DeepCopy\TypeFilterapplies a transformation to any element matched by
By design, matching a filter will stop the chain of filters (i.e. the next ones will not be applied).
Using the (
ChainableFilter) won't stop the chain of filters.
Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have any ID:
If you want a property to remain untouched (for example, an association to an object):
If you use cloning on proxy classes, you might want to apply two filters for:
- loading the data
- applying a transformation
You can use the
ChainableFilter as a decorator of the proxy loader filter, which won't stop the chain of filters (i.e.
the next ones may be applied).
If you use Doctrine and want to copy an entity, you will need to use the
If you use Doctrine and want to copy an entity who contains a
Collection that you want to be reset, you can use the
If you use Doctrine and use cloning on lazy loaded entities, you might encounter errors mentioning missing fields on a
Doctrine proxy class (...\__CG__\Proxy).
You can use the
DoctrineProxyFilter to load the actual entity behind the Doctrine proxy class.
Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded
before other filters are applied!
We recommend to decorate the
DoctrineProxyFilter with the
ChainableFilter to allow applying other filters to the
cloned lazy loaded entities.
ReplaceFilter (type filter)
If you want to replace the value of a property:
- If you want to replace whole element:
$callback parameter of the
ReplaceFilter constructor accepts any PHP callable.
ShallowCopyFilter (type filter)
Stop DeepCopy from recursively copying element, using standard
The following structures cannot be deep-copied with PHP Reflection. As a result they are shallow cloned and filters are not applied. There is two ways for you to handle them:
- Implement your own
- Use a filter with a type matcher
DeepCopy is distributed under the MIT license.
Running the tests is simple:
Get professional support via the Tidelift Subscription.