Download the PHP package janklan/deepcopy without Composer

On this page you can find all versions of the php package janklan/deepcopy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package deepcopy

DeepCopy

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

Total Downloads Integrate

Table of Contents

  1. How
  2. Why
    1. Using simply clone
    2. Overriding __clone()
    3. With DeepCopy
  3. How it works
  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. Edge cases
  6. Contributing
    1. Tests

How?

Install with Composer:

Use it:

Why?

You use __clone() and implement the behavior yourself.

Now you're in for a big mess :(

Using simply clone

Overriding __clone()

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 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:

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

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

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.

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):

ChainableFilter (filter)

If you use cloning on proxy classes, you might want to apply two filters for:

  1. loading the data
  2. 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).

DoctrineCollectionFilter (filter)

If you use Doctrine and want to copy an entity, you will need to use the DoctrineCollectionFilter:

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 the DoctrineEmptyCollectionFilter

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 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)

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

  2. If you want to replace whole element:

The $callback parameter of the ReplaceFilter constructor accepts any PHP callable.

ShallowCopyFilter (type filter)

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

Persisting cloned Doctrine entities

If you're cloning Doctrine entities and are not automatically cascading the persist operation, you have two options:

  1. Manually traverse your cloned association and persist new entities manually
  2. Use the DeepCopy::onObjectCopied callback to process each cloned object at the end of its cloning process.

Here is an example of the onObjectCopied callback that would persist your entities.

Edge cases

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:

Contributing

DeepCopy is distributed under the MIT license.

Tests

Running the tests is simple:

Acknowledgement

This is a fork of https://github.com/myclabs/DeepCopy/ - a massively popular library with millions downloads, which implies inherent legacy issues: it needs to support old code. At some stage I needed this library more than it did and the PR was too heavy for timely consideration.

I decided to fork the project with a few objectives:

  1. Drop older dependencies
  2. Bring the code up a bit, PHP 8.2 and up. If you need to clone complex objects using older software, please refer to https://github.com/myclabs/DeepCopy/. This will be the last significant commit made on 1.x branch in this janklan/deepcopy.
  3. Add functions that were missing - namely, the ability to automatically persist the cloned objects when cloning linked Doctrine objects
  4. As a vague goal, tweaking how the filters work. The way they are split between TypeFilter and (non-Type)Filter + the fact one can be chained and the other can't, it simply didn't sit well with me.

Thanks @mnapoli for all your work.


All versions of deepcopy with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1 || ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package janklan/deepcopy contains the following files

Loading the files please wait ....