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

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

stratadox/immutable-collection
==============================

v2(3y ago)51.8k9MITPHPPHP &gt;=8.0

Since Nov 11Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Stratadox/ImmutableCollection)[ Packagist](https://packagist.org/packages/stratadox/immutable-collection)[ RSS](/packages/stratadox-immutable-collection/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (8)Used By (9)

ImmutableCollection
===================

[](#immutablecollection)

[![Build Status](https://camo.githubusercontent.com/68ac6e2cc15bba61b8762906655a81d1cb85674c7affe2a9e43f13aeb745e1f4/68747470733a2f2f7472617669732d63692e6f72672f537472617461646f782f496d6d757461626c65436f6c6c656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Stratadox/ImmutableCollection)[![Coverage Status](https://camo.githubusercontent.com/1bf5c0f989929621a4c147c8214cb8d819ed228602a54db5294e376ad3ff699f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f537472617461646f782f496d6d757461626c65436f6c6c656374696f6e2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Stratadox/ImmutableCollection?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8adcaa63a0308d77cd4879498106ee486c3efb81adbebf25eab88ae6b3d8e9a1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f537472617461646f782f496d6d757461626c65436f6c6c656374696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Stratadox/ImmutableCollection/?branch=master)

Immutable implementation of the Collection interface.

Provides an abstract ImmutableCollection class, which extends (and restricts) the SplFixedArray class, as well as several traits with behavioural logic for (derived) immutable collection classes.

Why use this
------------

[](#why-use-this)

Regular php "arrays" are not really arrays at all, but sorted hash maps. They are not objects, they are not thread safe and they cannot guard their own invariants.

Although sorted hash maps are a versatile data structure that can be used in many different scenarios, they aren't always *good* data structures for the situation.

When dealing with a collection of objects, for instance, you probably want dedicated collection objects, on which you can call methods.

Many frameworks provide some sort of collection class. They rarely provide immutability, though, and are therefore not thread safe. Additionally, they are usually implementing a bloated interface with all of the methods you might some day need - but probably won't.

Due to the many optional expansions which can be applied at will, this package enables powerful typesafe collections that can be fine-tunes for use in any project, without getting distracted by a bloated collection interface.

The immutability of the collection means they can be used in projects that care about reducing state changes.

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

[](#installation)

Install using composer:

```
composer require stratadox/immutable-collection

```

Basic usage
-----------

[](#basic-usage)

The most basic implementation of the immutable collection might look something like the following:

```
