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
Php Cloneable

Php Cloneable Laravel Package

spatie/php-cloneable

Trait for PHP 8.1+ that makes objects with readonly properties cloneable. Safely “clone with changes” by copying an object while overriding readonly fields—handy until PHP gets native clone-with support.

View on GitHub
Deep Wiki
Context7

Clone readonly properties in PHP 8.1

Frequently asked questions about Php Cloneable
How do I use this package to clone objects with readonly properties in Laravel?
Add the `Cloneable` trait to your class and mark properties with the `#[Cloneable]` attribute. For example, in a Laravel model or DTO, use `use Cloneable;` and annotate readonly properties like `#[Cloneable] public readonly string $title`. The trait handles the cloning logic automatically during `__clone()`.
Does this work with Laravel Eloquent models that have readonly attributes?
Yes, this package works seamlessly with Eloquent models using PHP 8.1+ readonly attributes. Simply add the trait and attribute to your model, and you can clone it while modifying readonly properties. No additional Eloquent configuration is required.
What Laravel versions and PHP versions are supported?
This package requires PHP 8.1+ due to readonly property support. It works with Laravel 8.0+ and later. For older PHP versions, the trait gracefully degrades to a no-op, meaning it won’t interfere with your code but won’t provide cloning functionality either.
Will this break existing __clone() methods in my classes?
No, the `Cloneable` trait integrates with existing `__clone()` methods. If your class already defines `__clone()`, the trait will call it first before handling readonly properties. This ensures backward compatibility while adding the new functionality.
How do I test cloned objects to ensure they’re truly independent?
After cloning, modify a readonly property in the cloned object and verify the original object remains unchanged. For example, clone a `Post` object, update its `title`, and assert the original’s `title` hasn’t changed. Use PHPUnit assertions like `assertNotSame()` to check for deep independence.
Is this package suitable for performance-critical applications?
Cloning objects with many readonly properties may introduce overhead. For performance-sensitive applications, consider using the trait selectively on high-impact objects or implementing lazy cloning strategies. Benchmark cloning operations in your specific use case to evaluate the impact.
Can I use this with Laravel Collections or iterators that rely on cloning?
Yes, this package works well with Laravel Collections when cloning is required internally (e.g., during `map()` operations). However, ensure that any third-party libraries or custom iterators that assume shallow copies are updated to handle deep cloning if needed.
What alternatives exist for cloning objects with readonly properties?
Alternatives include manually implementing `__clone()` with reflection-based property copying or using serialization (e.g., `json_decode(json_encode($obj))`). However, these approaches are more verbose and error-prone. This package provides a standardized, maintainable solution tailored for PHP 8.1+.
How do I handle circular references when cloning objects?
The `Cloneable` trait does not automatically handle circular references (e.g., a `User` referencing its `posts` and vice versa). For such cases, you may need to implement custom logic in your `__clone()` method or use additional packages like `spatie/php-circular-reference-handler` to manage deep cloning.
Does this package work with Spatie’s other Laravel packages like `laravel-data` for DTOs?
Absolutely. This package is designed to work alongside Spatie’s `laravel-data` for DTOs and other immutable objects. Simply add the `Cloneable` trait to your DTO classes to enable cloning with readonly properties, maintaining consistency with Spatie’s ecosystem.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport