Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Polyfill Deepclone

Polyfill Deepclone Laravel Package

symfony/polyfill-deepclone

Pure-PHP polyfill for the deepclone extension. Provides deepclone_to_array() and deepclone_from_array() plus exceptions to clone/serialize value graphs while preserving identity, references, cycles, and private state. Uses a cache-friendly array wire format.

View on GitHub
Deep Wiki
Context7

Symfony polyfill for the deepclone extension

Frequently asked questions about Polyfill Deepclone
How does this polyfill help with Laravel’s Eloquent model cloning (e.g., replicate() or fresh())?
This polyfill ensures Eloquent models with circular relations (e.g., `User->posts->author`) or private properties are cloned accurately without breaking references. It replaces unreliable `serialize/unserialize` hacks, preserving object identity and state during operations like `replicate()` or `fresh()`. For nested relations, use `deepclone_to_array()` and `deepclone_from_array()` to serialize/deserialize safely.
Can I use this for Livewire components to maintain shared state across clones?
Yes. Enable the `DEEPCLONE_HYDRATE_PRESERVE_REFS` flag to maintain shared references in Livewire component clones, ensuring reactivity and state consistency. This avoids the overhead of recreating objects while preserving circular dependencies. Test with your component’s data structure to confirm memory efficiency.
What’s the performance impact compared to PHP 8.3’s native deepclone?
The polyfill is 4–5x slower than the native `deepclone` extension but remains viable for non-critical paths (e.g., caching, testing). Benchmark your Laravel app’s hot paths (e.g., model hydration) to decide if upgrading to PHP 8.3+ is worth the switch. Use feature detection to auto-fallback to the polyfill if native deepclone is unavailable.
How do I install and configure this for Laravel?
Install via Composer: `composer require symfony/polyfill-deepclone`. No Laravel-specific config is needed—use the functions directly: `deepclone_to_array($model)` or `deepclone_from_array($data)`. For global behavior, set `DEEPCLONE_HYDRATE_PRESERVE_REFS` in your `.env` or bootstrap files. Avoid enabling it in real-time APIs without profiling.
Will this break custom __clone() methods in my Laravel models or DTOs?
It may. The polyfill bypasses `__clone()` entirely to preserve references and cycles. Audit your codebase for custom `__clone()` logic and replace it with explicit handling (e.g., `deepclone_to_array()` + `deepclone_from_array()`) if conflicts arise. Test with your DTOs and models to ensure compatibility.
How does this handle SPL objects (e.g., SplFileInfo, SplObjectStorage) in Laravel?
The polyfill supports SPL objects but may fail on unserializable resources (e.g., file streams). For SPL-specific use cases, whitelist allowed classes via the `$allowed_classes` parameter. Test with Laravel’s file upload handlers or custom collections to validate behavior. Avoid using it for objects with `__wakeup()` or `__sleep()` methods that conflict with the polyfill’s logic.
Is this safe for Laravel’s cache layer (e.g., Redis, database)?
Yes, but with caveats. Use `deepclone_to_array()` to serialize objects into cache-friendly arrays, then `deepclone_from_array()` to restore them. This avoids memory spikes from repeated cloning. For large payloads (e.g., audit logs), monitor the 1M metadata limit to prevent crashes. Disable `DEEPCLONE_HYDRATE_PRESERVE_REFS` if caching shared references isn’t critical.
What alternatives exist for deep cloning in Laravel?
Alternatives include: 1) Native `deepclone` (PHP 8.3+), 2) Manual `serialize/unserialize()` (unreliable for cycles/private state), 3) Libraries like `dnoegel/php-xdg-base-dir` (for specific use cases), or 4) Custom recursive cloners. This polyfill is the most robust for Laravel’s object-heavy ecosystem, especially for Eloquent, Livewire, and caching. Benchmark alternatives if performance is critical.
How do I test this polyfill in a Laravel CI pipeline?
Add tests for: 1) Circular references in Eloquent models (e.g., `User->posts->user`), 2) Private property preservation, 3) SPL object handling, and 4) the 1M metadata limit with large datasets. Use Laravel’s `clone` helper or `deepclone_to_array()` in unit tests. Mock the polyfill’s functions to isolate behavior. Example: `assertSame($original->id, deepclone($original)->id);`.
Should I enable DEEPCLONE_HYDRATE_PRESERVE_REFS globally or opt-in?
Opt-in is safer. Enable it only for specific use cases like Livewire components, caching layers, or DTOs where shared references are needed. Global enablement risks memory leaks in high-frequency loops (e.g., WebSocket handlers) or real-time APIs. Start with DTOs or caching, then expand based on profiling results. Use environment variables (e.g., `.env`) to toggle it dynamically.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle