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

Product Decisions This Supports

  • Deep Cloning for State Management: Enables seamless cloning of Laravel models or DTOs with readonly properties (PHP 8.1+), critical for scenarios like:
    • Draft/snapshot functionality (e.g., "Save as Draft" in CMS or form builders).
    • Undo/redo operations (e.g., collaborative editors, design tools).
    • Immutable data patterns (e.g., caching original state for comparisons).
  • Roadmap Alignment: Supports migration to PHP 8.1+ while maintaining backward compatibility for cloning logic. Reduces technical debt by avoiding custom cloning workarounds (e.g., __clone() overrides).
  • Build vs. Buy: Buy—avoids reinventing cloning logic for readonly properties. Low maintenance cost (MIT-licensed, actively updated by Spatie).
  • Use Cases:
    • E-commerce: Clone carts/orders for "what-if" scenarios (e.g., "Apply discount to this copy").
    • SaaS Platforms: Isolate tenant data for sandboxing or testing.
    • Data Pipelines: Create immutable copies of records for audit trails or diffing.

When to Consider This Package

  • Adopt if:
    • Your Laravel/PHP 8.1+ app requires deep cloning of objects with readonly properties (e.g., Eloquent models, DTOs, or value objects).
    • You’re migrating to PHP 8.1+ and need a reliable, tested solution for cloning edge cases.
    • Your team lacks time to debug PHP’s readonly cloning quirks (see Stitcher.io’s deep dive).
    • You prioritize cleaner code over manual __clone() implementations or serialization/deserialization hacks.
  • Look elsewhere if:
    • You’re not using PHP 8.1+ (package is PHP 8.1-specific).
    • Your cloning needs are trivial (e.g., simple arrays or non-readonly objects).
    • You require performance-critical cloning (this trait adds minimal overhead but isn’t optimized for bulk operations).
    • Your objects have complex circular references (consider serialize()/unserialize() or libraries like jms/serializer).

How to Pitch It (Stakeholders)

For Executives: "This package lets us clone objects with readonly properties in PHP 8.1—critical for features like drafts, undo/redo, or data isolation—without custom engineering. It’s a low-risk, high-reward upgrade: Spatie maintains it, it’s MIT-licensed, and it saves dev time on cloning edge cases. For example, it could enable a ‘clone cart’ feature in our e-commerce platform with minimal effort."

For Engineering: "The Cloneable trait from Spatie solves PHP 8.1’s readonly property cloning issue elegantly. Instead of writing fragile __clone() methods or serializing objects, we add one trait to our models/DTOs. It’s battle-tested (used in Spatie’s other packages), has zero dependents (so no hidden risks), and aligns with our PHP 8.1 migration. Let’s use it for [specific use case, e.g., ‘user profile snapshots’] to avoid reinventing the wheel."

For Developers: "Need to clone a model with readonly fields? Just use use Cloneable. No more errors like ‘Cannot assign to property X in clone’. Works out of the box with Laravel Eloquent or plain PHP objects. Docs are clear, and Spatie’s support is reliable."

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