Download the PHP library myclabs/deep-copy

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.

Please rate this library. Is it a good library?

All versions of deep-copy with dependencies

Version
Requires php Version ^7.1

Informations about the package deep-copy

DeepCopy

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

Build Status Coverage Status Scrutinizer Quality Score Total Downloads

Table of Contents

  1. Installation
  2. Why
    1. Using simply clone
    2. Overridding __clone()
    3. With DeepCopy
  3. Usage
  4. Going further
    1. Matchers
      1. Property name
      2. Specific property
      3. Type
    2. Filters
      1. SetNullFilter
      2. KeepFilter
      3. DoctrineCollectionFilter
      4. DoctrineEmptyCollectionFilter
      5. DoctrineProxyFilter
      6. ReplaceFilter
      7. ShallowCopyFilter
  5. Contributing
    1. Tests

Installation

With Composer:

Why?

You use __clone() and implement the behavior yourself.

Now you're in for a big mess :(

Using simply clone

Overridding __clone()

With DeepCopy

Usage

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:

Or you may want to roll your own deep copy function:

Going further

You can add filters to customize the copy process by adding filters:

During the copy process, when a property is matched by a filter associated to this matcher is applied. By default, when a filter is applied this stops the process, i.e. the next matcher-filter pair will not be checked, unless the filter is implemented as a chainable filter.

Some generic filters and matchers are already provided.

Matchers

Property name

The PropertyNameMatcher will match a property by its name:

Specific property

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

Type

The TypeMatcher will match any element by its type (instance of a class or any value that could be parameter of gettype() function):

Filters

Except a few exceptions (when the filter is a chainable filter like DoctrineProxyFilter), matching a filter will stop the chain of filters (i.e. the next ones will not be applied).

SetNullFilter (filter)

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:

KeepFilter (filter)

If you want a property to remain untouched (for example, an association to an object):

DoctrineCollectionFilter (filter)

This filters allows to copy a Doctrine entity:

DoctrineEmptyCollectionFilter (filter)

If you use Doctrine and want to copy an entity who contains a Collection that you want to be reset, you can use this filter:

DoctrineProxyFilter (filter)

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 this filter 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!

This filter won't stop the chain of filters (i.e. the next ones may be applied).

ReplaceFilter (type filter)

  1. If you want to replace the value of a property:

  2. If you want to replace whole element:

ShallowCopyFilter (type filter)

Stop DeepCopy from recursively copying element, using standard clone instead:

Contributing

This package is distributed under the MIT license.

Tests

Running the tests is simple:


The package myclabs/deep-copy contains the following files

Loading the files please wait ....