PHPackages                             spatie/php-cloneable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/php-cloneable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/php-cloneable
====================

A trait that allows you to clone readonly properties in PHP 8.1

1.0.2(2y ago)971.2M↑23.3%811MITPHPPHP ^8.1CI passing

Since Nov 15Pushed 8mo ago3 watchersCompare

[ Source](https://github.com/spatie/php-cloneable)[ Packagist](https://packagist.org/packages/spatie/php-cloneable)[ Docs](https://github.com/spatie/php-cloneable)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-php-cloneable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (11)

A trait that allows you to clone readonly properties in PHP 8.1
===============================================================

[](#a-trait-that-allows-you-to-clone-readonly-properties-in-php-81)

[![Latest Version on Packagist](https://camo.githubusercontent.com/55c010afab8b5262af9a8ab3887ddda03ff464d1e97e56a6c43c7a6d5f894278/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f7068702d636c6f6e6561626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/php-cloneable)[![GitHub Tests Action Status](https://camo.githubusercontent.com/76b095a53c49493a860d84bc93add26f22c5978cee4b2dd7e7aba1e3a363dbb7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f7068702d636c6f6e6561626c652f72756e2d74657374732e796d6c3f6c6162656c3d7465737473266272616e63683d6d61696e)](https://github.com/spatie/php-cloneable/actions?query=workflow%3ATests+branch%3Amaster)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/08d9d601d60cc7c3ef87ca256a787071cd7bf553d626c8224ec669ce8edfb87a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f7068702d636c6f6e6561626c652f7068702d63732d66697865722e796d6c3f6c6162656c3d636f64652532307374796c65266272616e63683d6d61696e)](https://github.com/spatie/php-cloneable/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/d15969fd1492146dabb82a73bad8f0322c7ab29ea8a2eebdb28439a156a1248a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f7068702d636c6f6e6561626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/php-cloneable)

This package provides a trait that allows you to clone objects with readonly properties in PHP 8.1. You can read an in-depth explanation as to why this is necessary [here](https://stitcher.io/blog/cloning-readonly-properties-in-php-81).

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/2520e5e6c24b762dab498de270867c10f78a542d9917447fd404a54c6f00c9ee/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f7068702d636c6f6e6561626c652e6a70673f743d31)](https://spatie.be/github-ad-click/php-cloneable)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/php-cloneable
```

Usage
-----

[](#usage)

In PHP 8.1, readonly properties aren't allowed to be overridden as soon as they are initialized. That also means that cloning an object and changing one of its readonly properties isn't allowed. It's likely that PHP will get some kind of `clone with` functionality in the future, but for now you can work around this issue by using this package.

```
class Post
{
    use Cloneable;

    public readonly string $title;

    public readonly string $author;

    public function __construct(string $title, string $author)
    {
        $this->title = $title;
        $this->author = $author;
    }
}
```

The `Spatie\Cloneable\Cloneable` adds a `with` method to whatever class you want to be cloneable, which you can pass one or more parameters. Note that you're required to use named arguments.

```
$postA = new Post(title: 'a', author: 'Brent');

$postB = $postA->with(title: 'b');
$postC = $postA->with(title: 'c', author: 'Freek');
```

A common practice would be to implement specific `with*` methods on the classes themselves:

```
class Post
{
    // …

    public function withTitle(string $title): self
    {
        return $this->with(title: $title);
    }

    public function withAuthor(string $author): self
    {
        return $this->with(author: $author);
    }
}
```

### Caveats

[](#caveats)

- This package will skip calling the constructor when cloning an object, meaning any logic in the constructor won't be executed.
- The `with` method will do a shallow clone, meaning that nested objects aren't cloned as well.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Brent Roose](https://github.com/brendt_gd)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance43

Moderate activity, may be stable

Popularity54

Moderate usage in the ecosystem

Community30

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~406 days

Total

3

Last Release

832d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (10 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (6 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (5 commits)")[![abenerd](https://avatars.githubusercontent.com/u/7523903?v=4)](https://github.com/abenerd "abenerd (4 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![ArtARTs36](https://avatars.githubusercontent.com/u/8027278?v=4)](https://github.com/ArtARTs36 "ArtARTs36 (1 commits)")[![andrewnicols](https://avatars.githubusercontent.com/u/370047?v=4)](https://github.com/andrewnicols "andrewnicols (1 commits)")[![gehrisandro](https://avatars.githubusercontent.com/u/25097194?v=4)](https://github.com/gehrisandro "gehrisandro (1 commits)")[![giggsey](https://avatars.githubusercontent.com/u/305730?v=4)](https://github.com/giggsey "giggsey (1 commits)")[![klkvsk](https://avatars.githubusercontent.com/u/1466771?v=4)](https://github.com/klkvsk "klkvsk (1 commits)")

---

Tags

spatiephp\_cloneable

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/spatie-php-cloneable/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-php-cloneable/health.svg)](https://phpackages.com/packages/spatie-php-cloneable)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
