PHPackages                             jasny/immutable - 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. jasny/immutable

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

jasny/immutable
===============

Helper methods for immutable objects

v2.1.0(6y ago)450.4k↑669.3%13MITPHPPHP &gt;=7.2.0

Since Mar 15Pushed 4y ago1 watchersCompare

[ Source](https://github.com/jasny/immutable)[ Packagist](https://packagist.org/packages/jasny/immutable)[ RSS](/packages/jasny-immutable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (6)Used By (3)

Jasny immutable
===============

[](#jasny-immutable)

[![Build Status](https://camo.githubusercontent.com/5a6c0d091d7f1a04078107ee591be5c79ff622f29ab04d6c10c3c6ea5ca5db70/68747470733a2f2f7472617669732d63692e6f72672f6a61736e792f696d6d757461626c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jasny/immutable)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c2f7add850ed8a95befaf186aed275f427d81b9f75af4d4d4132ac73df398bff/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f696d6d757461626c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/immutable/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d3819f637962291ff569b156613f30975f249e203348f9eff4ff2d685b87f10e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f696d6d757461626c652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/immutable/?branch=master)[![Packagist Stable Version](https://camo.githubusercontent.com/783120e002c3084d7869905f3f7eedadcc0027e956c45fd665ea8e60a2fc1671/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f696d6d757461626c652e737667)](https://packagist.org/packages/jasny/immutable)[![Packagist License](https://camo.githubusercontent.com/8a8af4523404f97417cdd15e11dd6d12d941aec430dfef75094bef98473eabb9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f696d6d757461626c652e737667)](https://packagist.org/packages/jasny/immutable)

The library provides a helper methods for immutable objects.

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

[](#installation)

```
composer require jasny/immutable

```

Usage
-----

[](#usage)

### NoDynamicProperties

[](#nodynamicproperties)

The `Jasny\Immutable\NoDynamicProperties` defines the `__set` method, which will throw a `LogicException` when attempting to set a non-existing property.

```
class ImmutableFoo
{
    use Jasny\Immutable\NoDynamicProperties;
}
```

### With

[](#with)

Immutable objects may have `with...` methods, that create an altered version of the object, while the immutable object itself remains unchanged. An example are classes work as described in [PSR-7](https://www.php-fig.org/psr/psr-7/).

The `with...` methods in these objects typically follow the same method.

```
class ImmutableFoo
{
    public function withSomething(string $newValue)
    {
        if ($this->something === $newValue) {
            return $this;
        }

        $clone = clone $this;
        $clone->something = $newValue;

        return $clone;
    }
}
```

The `Jasny\Immutable\With` trait implements a protected `withProperty()` and `withoutProperty()` method for setting and unsetting properties on a clone of the object.

```
class ImmutableFoo
{
    use Jasny\Immutable\With;

    protected $something;

    public function withSomething(string $value): self
    {
        return $this->withProperty('something', $value);
    }

    public function withoutSomething(): self
    {
        return $this->withoutProperty('something');
    }
}
```

The trait contains the `withPropertyKey()` and `withoutPropertyKey()` methods for setting and unsetting an item of an associative array.

```
class ImmutableFoo
{
    use Jasny\Immutable\With;

    protected array $colors = [];

    public function withColor(string $color, int $level): self
    {
        return $this->withPropertyKey('colors', $color, $level);
    }

    public function withoutColor(string $color): self
    {
        return $this->withoutPropertyKey('colors', $color);
    }
}
```

The `withPropertyItem()` and `withoutPropertyItem()` methods work on a sequential array to add and remove an item.

```
class ImmutableFoo
{
    use Jasny\Immutable\With;

    protected array $services = [];

    public function withAddedService($service): self
    {
        return $this->withPropertyItem('services', $service, true /* unique */);
    }

    public function withoutService($service): self
    {
        return $this->withoutPropertyItem('services', $service);
    }
}
```

If the third argument of `withPropertyItem()` is set to `true`, the item isn't added if it's already in the array.

If the item is in the array multiple times, `withoutPropertyItem()` will remove them all. Strict comparison is used to find items.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~105 days

Total

4

Last Release

2304d ago

Major Versions

v1.0.0 → v2.0.02019-09-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (11 commits)")

---

Tags

immutable-objectsphp7trait

### Embed Badge

![Health badge](/badges/jasny-immutable/health.svg)

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

###  Alternatives

[bensampo/laravel-embed

Painless responsive embeds for videos, slideshows and more.

142146.8k](/packages/bensampo-laravel-embed)

PHPackages © 2026

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