- What is myclabs/deep-copy used for in PHP?
- It creates deep copies of PHP objects by recursively copying referenced objects and preserving the object graph. It is cycle-safe because it maps each source object to its copy. In Laravel it suits DTOs, value objects, snapshots, test fixtures and duplicate-as-template flows.
- How do I install myclabs/deep-copy in a Laravel project?
- Run composer require myclabs/deep-copy. No service provider, config file, migrations or environment variables are needed. It is often already present transitively through PHPUnit, but you should add it as a direct dependency if production code uses it.
- Does myclabs/deep-copy support Laravel 10 or Laravel 11?
- It is framework-agnostic and does not ship Laravel integration, so support depends mainly on the PHP version. Version 1.14.0 requires PHP 8.0 or newer, which fits Laravel 9 and later. On older PHP 7 branches you would be pinned to 1.13.x.
- Do I need a service provider or config for DeepCopy in Laravel?
- No, DeepCopy has no Laravel service provider or configuration file. The practical approach is to build a configured DeepCopy instance in one of your own service providers and inject it where needed. Wrap it in a small application service so call sites do not depend directly on the vendor API.
- How does DeepCopy handle circular references between objects?
- It keeps an identity map from each source object to its copy. When it encounters an object already copied, it reuses the existing copy instead of recursing again. That preserves cycles and shared references instead of causing infinite recursion.
- Can DeepCopy clone Eloquent models with loaded relationships?
- It can traverse them, but that is usually not what you want. Eloquent models can reach connection, event dispatcher and relation state, so a deep copy may be expensive or hit uncloneable internals. The bundled Doctrine filters do not map to Eloquent relations. Scope DeepCopy to plain DTOs and value objects, and use explicit replication logic for Eloquent entities.
- Why does DeepCopy not call __clone() on my objects?
- DeepCopy copies properties through reflection rather than using PHP clone, so __clone side effects do not run. UUID regeneration, timestamp resets, cache clearing and collaborator rebinding will be silently skipped. Audit classes that rely on __clone before copying them in production.
- How do matchers and filters work in myclabs/deep-copy?
- Matchers decide which properties DeepCopy should treat specially, such as by property name, a specific property or a type. Filters define what happens to a matched property, for example set it to null, keep the original value or replace it. You register them on the DeepCopy instance so your application controls copy behavior consistently.
- What happens with readonly properties and typed properties in DeepCopy?
- Version 1.12 and later deliberately skips readonly properties during copying. Typed properties that are uninitialized are not re-validated, and constructor invariants are not re-run. A copy can therefore differ from the original without an error, so test these classes explicitly.
- Is myclabs/deep-copy safe for production use, and what are the alternatives?
- Yes, it is a mature, MIT-licensed library with broad transitive adoption, but its risks are behavioral rather than availability-related. Avoid copying graphs that include PDO handles, resources, container references or other opaque infrastructure state. Define a copy-safe boundary and cover it with tests. Alternatives include manual copy constructors, explicit __clone logic for simple graphs, or serialization-based copying where the graph supports it.