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

ActiveLibrary

immutable/immutable
===================

DI Container

v1.0.2(8y ago)014proprietaryPHPPHP ^7.0

Since Oct 28Pushed 8y ago1 watchersCompare

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

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

Immutable object for PHP
========================

[](#immutable-object-for-php)

[![CircleCI](https://camo.githubusercontent.com/8ab49564a8d87020e30063d173e91f04b5249deac1a42dbfc6d17525cb9e3b8b/68747470733a2f2f636972636c6563692e636f6d2f67682f73757a756e6f6e652f496d6d757461626c655048502e7376673f7374796c653d737667)](https://circleci.com/gh/suzunone/ImmutablePHP)

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

[](#introduction)

An Immutable object is an object that can not be changed once it is created.

Using Immutable object makes it unnecessary to clone an object for precaution in order to avoid unintentional object changes, so the code is simple.

Normally, there is a need to design a class so that it becomes Immutable from the beginning, but if you use Immutable, you can easily make an existing object Immutable.

Easy to use
-----------

[](#easy-to-use)

Using composer, it can be implemented very easily.

```
composer require immutable/immutable

```

Immutable
---------

[](#immutable)

Immutable objects are created from existing objects or arrays.

If an array is used, it is internally converted to \\ stdClass.

Even if you change to Immutable object, you can use all public methods and properties.

In addition, even when using a private (or protected) method or property in a public method, it also works.

### A simple example

[](#a-simple-example)

With Immutable, Immutable objectization is very easy.

The following example imtutables a DateTime object.

```
format('Y-m-d H:i:s');
// 2017-10-09 12:20:30

$DT->modify('+10 day');

echo $DT->format('Y-m-d H:i:s');
// 2017-10-09 12:20:30
```

ImmutableElement
----------------

[](#immutableelement)

If stdClass (or array) is Immutable object, it will eventually be converted as ImmutableElement.

ImmutableElement provides a simple API.

```
 1, 'b' => 2, 'c' => 3, ]);
$element2 = $element1->set('b', 100);
echo $element1->get('b');
// 2

// Or do as follows...
echo $element1->b;
// 2

echo $element2->get('b');
// 100

// Or do as follows...
echo $element2->b;
// 100
```

### Method Chain

[](#method-chain)

If the original object is using a method chain, create a new Immutable object with changes in Method added.

```
require 'vendor/autoload.php';

use Immutable\Immutable;

$DT = new DateTime('2017-10-09 12:20:30');

// Immutable objectization
$DT = Immutable::freeze($DT);

echo $DT->format('Y-m-d H:i:s');
// 2017-10-09 12:20:30

// A new Immutable object that is + 10 days is substituted for $DT2.
$DT2 = $DT->modify('+10 day');
echo $DT2->format('Y-m-d H:i:s');
// 2017-10-19 12:20:30

// With a method chain.
echo $DT->modify('+10 day')->format('Y-m-d H:i:s');
// 2017-10-19 12:20:30

// Immutable object does not change
echo $DT->format('Y-m-d H:i:s');
// 2017-10-09 12:20:30
```

Recursive Immutable
===================

[](#recursive-immutable)

When Immutable an array or stdClass, you would want to recursively make all child elements Immutable.

A mechanism for that purpose is also prepared.

The following example is an example of recursively Immutable multidimensional associative array.

```
require 'vendor/autoload.php';

use Immutable\Immutable;

$array_mutable = [['name' => 'Tanaka'], ['name' => 'Suzuki']];
$array_immutable = Immutable::freezeRecursive($array_mutable);
```

Mutable
-------

[](#mutable)

It is also easy to return an immutable object to a mutable object.

### A simple example

[](#a-simple-example-1)

Mutable is also very simple. The following example restores the Imutableized DateTime object.

```
format('Y-m-d H:i:s');
// 2017-10-09 12:20:30

$DT->modify('+10 day');

echo $DT->format('Y-m-d H:i:s');
// 2017-10-19 12:20:30
```

instanceOf
----------

[](#instanceof)

Immutable objects are naturally objects different from mutable objects, so you can not examine them with the native `instanceof` operator.

It shows an alternative means for that.

### Example using `Immutable\Immutable::instanceOf()`

[](#example-using-immutableimmutableinstanceof)

Here is an example of using `Immutable\Immutable::instanceOf()` Method to make an `instanceof` judgment.

```
modify('+'.$int.' day');
    }
}

class DateTimeImmutableInstance extends ImmutableInstance implements DateTimeClassInterface
{
    public function subDay(int $int) : DateTimeClassInterface
    {
        // When implementing the contents of interface please describe all contents below
        return call_user_func_array([$this, '__call'], [__FUNCTION__, func_get_args()]);
    }
}

$DT = new DateTimeInstance('2017-10-09 12:20:30');

// Immutable (specify the class name with the second argument)
$DT = Immutable::freeze($DT, DateTimeImmutableInstance::class);

// Class name obviously true
var_export(Immutable::instanceOf($DT, DateTimeInstance::class));
// true

// As with instanceof, even if you specify an impleted interface, it is true
var_export(Immutable::instanceOf($DT, DateTimeClassInterface ::class));
// true

// Since Interface is common, it is true
var_export($DT instanceof DateTimeImmutableInstance);
// true

// Functions and methods with type hintings can also be used

function example(DateTimeClassInterface $instance)
{
    return 'ok';
}

echo example($DT);
// ok

// Implemented methods
var_export($DT->subDay(10)->format('Y-m-d H:i:s'));
// '2017-10-19 12:20:30'

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

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

Total

3

Last Release

3119d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/630015fa1a1a301f4fdebfc315ac1e2b6ad90ae681972e0d067c6887a7ed292d?d=identicon)[suzunone](/maintainers/suzunone)

---

Top Contributors

[![suzunone](https://avatars.githubusercontent.com/u/7632752?v=4)](https://github.com/suzunone "suzunone (25 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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