PHPackages                             matthiasnoback/convenient-immutability - 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. matthiasnoback/convenient-immutability

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

matthiasnoback/convenient-immutability
======================================

Make objects initially inconsistent, yet eventually immutable

v0.3.0(6y ago)7865.6k↓36%5MITPHPPHP &gt;=7.2CI failing

Since Jan 12Pushed 6y ago5 watchersCompare

[ Source](https://github.com/matthiasnoback/convenient-immutability)[ Packagist](https://packagist.org/packages/matthiasnoback/convenient-immutability)[ RSS](/packages/matthiasnoback-convenient-immutability/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (4)Versions (5)Used By (0)

Convenient immutability for (some) PHP objects
==============================================

[](#convenient-immutability-for-some-php-objects)

**Warning: I didn't use this library "in the wild" - please let me know if it works in your situation**

[![Build Status](https://camo.githubusercontent.com/2ae0c33981795ce7faab308270b49d3ac66004194e5edeecd386dcb6857327ff/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d617474686961736e6f6261636b2f636f6e76656e69656e742d696d6d75746162696c6974792e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/matthiasnoback/convenient-immutability)[![Coverage Status](https://camo.githubusercontent.com/90c2bb2342e7d43832f7a6bb617085be7231c310b43c5fbc3a3720342843bf03/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6d617474686961736e6f6261636b2f636f6e76656e69656e742d696d6d75746162696c6974792e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/github/matthiasnoback/convenient-immutability)[![Latest Version on Packagist](https://camo.githubusercontent.com/2f413aa67ec481a1b20809f2e036a27dc1590055691f1913dc27b6da593c4d05/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d617474686961736e6f6261636b2f636f6e76656e69656e742d696d6d75746162696c6974792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/matthiasnoback/convenient-immutability)[![Total Downloads](https://camo.githubusercontent.com/27d4d334384d04cfe0ebd0dcc872cbd0448a46772638574246da51c0e03e5d28/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d617474686961736e6f6261636b2f636f6e76656e69656e742d696d6d75746162696c6974792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/matthiasnoback/convenient-immutability)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

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

[](#installation)

Just run:

```
composer require matthiasnoback/convenient-immutability

```

Introduction
------------

[](#introduction)

I've often encountered the following situation (in particular when working with [command objects](http://php-and-symfony.matthiasnoback.nl/2015/01/a-wave-of-command-buses/)).

1. I start with a simple object (in fact, a DTO), with just some public properties.
2. The Symfony Form component copies some values into the object's properties.
3. I copy some extra data into the object (like a generated UUID).
4. I then use that object as a command or query message, handing it over to some inner layer of my application.

In other words, I have a class that looks like this:

```
class OrderSeats
{
    public $id;
    public $userId;
    public $seatNumbers = [];
}
```

And use it like this:

```
$form = $this->factory->create(OrderSeatsFormType::class);
$form->submit($request);
$command = $form->getData();
$command->id = Uuid::uuid4();

$commandBus->handle($command);
```

Then **I want it to be impossible to change any field on the (command) object**, making it effectively *immutable*.

The `ConvenientImmutability\Immutable` trait solves this problem. When your class uses this trait, it:

- Allows form components and the likes to put anything in your object in any particular order they like.
- Allows you to get the data from it by simply accessing its (public) variables.
- Doesn't allow anyone to overwrite previously set data.

Why would you do this?
----------------------

[](#why-would-you-do-this)

- To feel less insecure about using public properties for your desirably immutable objects.
- To prevent accidental writes to objects you assumed were immutable.

What's the problem with this approach?
--------------------------------------

[](#whats-the-problem-with-this-approach)

- Your objects may be immutable...
- but they can still exist in an inconsistent state.

Is this bad? I don't think so. As long as you validate the objects (e.g. using the Symfony Validator) and then throw them into your domain layer, which contains the actual safeguards against inconsistent state.

(You can also just use public properties and treat them as "set once, never again" in other parts of your application.)

License
-------

[](#license)

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

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 62.5% of commits — single point of failure

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

Total

4

Last Release

2343d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1193078?v=4)[Matthias Noback](/maintainers/matthiasnoback)[@matthiasnoback](https://github.com/matthiasnoback)

---

Top Contributors

[![matthiasnoback](https://avatars.githubusercontent.com/u/1193078?v=4)](https://github.com/matthiasnoback "matthiasnoback (15 commits)")[![jdreesen](https://avatars.githubusercontent.com/u/424602?v=4)](https://github.com/jdreesen "jdreesen (4 commits)")[![dzubchik](https://avatars.githubusercontent.com/u/2685761?v=4)](https://github.com/dzubchik "dzubchik (3 commits)")[![mdi22](https://avatars.githubusercontent.com/u/12232700?v=4)](https://github.com/mdi22 "mdi22 (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/matthiasnoback-convenient-immutability/health.svg)

```
[![Health](https://phpackages.com/badges/matthiasnoback-convenient-immutability/health.svg)](https://phpackages.com/packages/matthiasnoback-convenient-immutability)
```

###  Alternatives

[lupecode/php-trader-native

An native version of the PHP Trader extension.

108136.9k1](/packages/lupecode-php-trader-native)[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1455.6k](/packages/dmstr-yii2-cookie-consent)

PHPackages © 2026

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