Library to create PHP immutable object

3.0.1 2022-03-08 18:48 UTC


Latest Stable Version Latest Unstable Version Total Downloads License PHPStan

This library helps you to create immutable object by prohibiting __set and __unset calls and several call to constructor

Quick Example



include 'vendor/autoload.php';

$a = new class implements Teknoo\Immutable\ImmutableInterface {
    use Teknoo\Immutable\ImmutableTrait;

    private array $values = ['foo' => 123];

    public function __get(string $name)
        return $this->values[$name];

//Print 123
print $a->foo;
//Throws an Teknoo\Immutable\Exception\ImmutableException
$a->foo = 'bar';
//Throws an Teknoo\Immutable\Exception\ImmutableException;
//Throws an Teknoo\Immutable\Exception\ImmutableException;

Support this project

This project is free and will remain free, but it is developed on my personal time. If you like it and help me maintain it and evolve it, don't hesitate to support me on Patreon. Thanks :) Richard.

Installation & Requirements

To install this library with composer, run this command :

composer require teknoo/immutable

This library requires :

* PHP 8.1+

News from Teknoo Immutable 3.0

This library requires PHP 8.1 or newer. Some changes cause bc breaks :

  • Switch to readonly property to detect reconstructed object

News from Teknoo Immutable 2.0

This library requires PHP 7.4 or newer. Some changes cause bc breaks :

  • PHP 7.4 is the minimum required
  • Most methods have been updated to include type hints where applicable. Please check your extension points to make sure the function signatures are correct.
  • Switch to typed properties _ All files use strict typing. Please make sure to not rely on type coercion.
  • Remove some PHP useless DockBlocks
  • Enable PHPStan in QA Tools and disable PHPMd


Richard Déloge - richarddeloge@gmail.com - Lead developer. Teknoo Software - http://teknoo.software

About Teknoo Software

Teknoo Software is a PHP software editor, founded by Richard Déloge. Teknoo Software's DNA is simple : Provide to our partners and to the community a set of high quality services or software, sharing knowledge and skills.


Immutable is licensed under the MIT License - see the licenses folder for details

Contribute :)

You are welcome to contribute to this project. Fork it on Github