eboreum/immutable-interface

Flag a class as being immutable by implementing the ImmutableInterface. An immutable object must never change its state; i.e. its class properties must remain unchanged after instantiation or cloning.

Maintainers

Package info

gitlab.com/eboreum/immutable-interface

Issues

pkg:composer/eboreum/immutable-interface

Statistics

Installs: 7

Dependents: 1

Suggesters: 0

Stars: 0

1.0.0 2026-05-17 09:13 UTC

This package is auto-updated.

Last update: 2026-05-17 10:41:52 UTC


README

license pipeline status PHPStan Level

eboreum-immutable-interface

Implement Eboreum\ImmutableInterface\ImmutableInterface on any class to flag it as being immutable. An immutable object must never change its state; i.e. its class properties must remain unchanged after instantiation or cloning.

Normal inheritance applies. Meaning once a class or interface has implemented/extended Eboreum\ImmutableInterface\ImmutableInterface, every child class/interface must also be considered to be immutable.

While you are not allowed to change the state of an object (e.g. through set... methods), you may instead do so on a clone and then return said clone. This is typically done using with... methods, which should have the return type static.

An alternative

If you do not like the inheritance pattern or if you have specific cases where you do not want inheritance to apply, you may instead use these libraries in combination:

  1. php-static-analysis/attributes: Defines the @immutable annotation.
  2. php-static-analysis/phpstan-extension: A --dev package, which reacts to the annotations defined in php-static-analysis/attributes (bullet 1).

Requirements

"php": "^8.5"

Installation

Via Composer (https://packagist.org/packages/eboreum/immutable-interface):

composer require eboreum/immutable-interface

Via GitLab:

git clone git@gitlab.com:eboreum/immutable-interface.git

License & Disclaimer

See LICENSE file. Basically: Use this library at your own risk.

Contributing

We prefer that you create an issue and or a merge request at https://gitlab.com/eboreum/immutable-interface, and have a discussion about a feature or bug here.

Credits

Authors