PHPackages                             jclaveau/php-immutable-trait - 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. jclaveau/php-immutable-trait

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

jclaveau/php-immutable-trait
============================

A trait to implement easily (im)mutable objects

1.0.1(7y ago)329MITPHPPHP &gt;=5.6.0

Since Nov 4Pushed 7y ago3 watchersCompare

[ Source](https://github.com/jclaveau/php-immutable-trait)[ Packagist](https://packagist.org/packages/jclaveau/php-immutable-trait)[ RSS](/packages/jclaveau-php-immutable-trait/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

Immutable Trait
===============

[](#immutable-trait)

This trait makes it super easy to turn an instance immutable or mutable.

Quality
-------

[](#quality)

[![Build Status](https://camo.githubusercontent.com/239f45cdd674813892dc54ea8f5ccc5248a29dd757667a1591430bcb762229bf/68747470733a2f2f7472617669732d63692e6f72672f6a636c61766561752f7068702d696d6d757461626c652d74726169742e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/jclaveau/php-immutable-trait)[![codecov](https://camo.githubusercontent.com/81e50d7f587d1b4a4fb978fe846a9f0cdac16fc02a70f550b119c5a5f4d88b55/68747470733a2f2f636f6465636f762e696f2f67682f6a636c61766561752f7068702d696d6d757461626c652d74726169742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/jclaveau/php-immutable-trait)[![contributions welcome](https://camo.githubusercontent.com/9e93e892d0685e1bf7a1d0bd7c8410d6ecf2086a0a7b48dd58a6b96fa556ea2a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6e747269627574696f6e732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c6174)](https://github.com/jclaveau/php-immutable-trait/issues)[![Viewed](https://camo.githubusercontent.com/aaf3b04bed49ced41c635b68b9b8193c5a7897ae08971dff9947ffaf45a9d584/687474703a2f2f686974732e6477796c2e636f6d2f6a636c61766561752f7068702d696d6d757461626c652d74726169742e737667)](http://hits.dwyl.com/jclaveau/php-immutable-trait)

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

[](#installation)

php-immutable-trait is installable via [Composer](http://getcomposer.org)

```
composer require jclaveau/php-immutable-trait

```

Usage
-----

[](#usage)

```
class ImmutableObject
{
    use Immutable;
    // use SwitchableMutability; // This traits provides becomesMutable() and becomesImmutable()

    protected $property;

    public function setProperty($value)
    {
        // Just add these lines at the really beginning of methods supporting
        // immutability (setters mostly)
        if ($this->callOnCloneIfImmutable($result))
            return $result;

        // $this is now the new instance if it's immutable
        $this->property = $value;
        return $this;
    }

    public function getProperty()
    {
        return $this->property;
    }
}

$instance = new ImmutableObject;
$instance2 = $instance->setProperty('new value');

var_dump( $instance->getProperty() ); => null
var_dump( $instance2->getProperty() ); => 'new value'
```

TODO
----

[](#todo)

- Profiles
- PHP 7
- Support immutability for private / protected methods? Should the dev handle it? Should we provide a simple protected API for it?

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

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 ~0 days

Total

2

Last Release

2748d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cc5884f6e6eb1ed07e687cfbb421981d94dd9133e85220df1a546c7f78851bd7?d=identicon)[jclaveau](/maintainers/jclaveau)

---

Tags

clonetraitimmutablemutableinstancefunctionnal

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jclaveau-php-immutable-trait/health.svg)

```
[![Health](https://phpackages.com/badges/jclaveau-php-immutable-trait/health.svg)](https://phpackages.com/packages/jclaveau-php-immutable-trait)
```

###  Alternatives

[myclabs/deep-copy

Create deep copies (clones) of your objects

8.9k849.8M169](/packages/myclabs-deep-copy)[symfony/var-exporter

Provides tools to export, instantiate, hydrate, clone and lazy-load PHP objects

2.1k378.1M441](/packages/symfony-var-exporter)[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89152.7M321](/packages/nette-robot-loader)[aeon-php/calendar

PHP type safe, immutable calendar library

2079.7M16](/packages/aeon-php-calendar)[kkszymanowski/traitor

Add a trait use statement to existing PHP class

1305.2M16](/packages/kkszymanowski-traitor)[innmind/immutable

Immutable PHP primitive wrappers

75218.0k74](/packages/innmind-immutable)

PHPackages © 2026

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