PHPackages                             shrikeh/collections - 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. shrikeh/collections

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

shrikeh/collections
===================

Traits for re-use in immutable collections

v1.0.4(9y ago)11.5kMITPHP

Since Oct 13Pushed 9y ago2 watchersCompare

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

READMEChangelog (5)Dependencies (5)Versions (7)Used By (0)

php-immutable-collections
=========================

[](#php-immutable-collections)

[![build_status_img](https://camo.githubusercontent.com/4c62b1a52c006c44a5cad27abeac0833703865c244bc1f75286d9d264087a611/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736872696b65682f7068702d696d6d757461626c652d636f6c6c656374696f6e732e737667 "Build Status")](https://travis-ci.org/shrikeh/php-immutable-collections)[![code_quality_img](https://camo.githubusercontent.com/9a816de7f0fb0455d376e7dc225ba0b9c05194819c70e85dbe372603a529b7ca/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f736872696b65682f7068702d696d6d757461626c652d636f6c6c656374696f6e732e737667 "Scrutinizer Code Quality")](https://scrutinizer-ci.com/g/shrikeh/php-immutable-collections/?branch=master)[![latest_stable_version_img](https://camo.githubusercontent.com/6037d63ad293d62a5ffd28453c032ea4afe7dea577fc3497b6bf870fbf852915/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736872696b65682f636f6c6c656374696f6e732e737667 "Latest Stable Version")](https://packagist.org/packages/shrikeh/collections "Latest Stable Version")[![latest_unstable_version_img](https://camo.githubusercontent.com/afcc30a4b639a1d2fd5b2c6a46ee27b0302e7de796d9d13cda326e51d49a55c1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f736872696b65682f636f6c6c656374696f6e732e737667 "Latest Unstable Version")](https://packagist.org/packages/shrikeh/collections "Latest Unstable Version")[![license_img](https://camo.githubusercontent.com/32086bf8ecf5a6ae08bbc1571f4302f63980e681041522b3ae6af6c5215a8efe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f736872696b65682f636f6c6c656374696f6e732e737667 "License")](https://packagist.org/packages/shrikeh/collections)[![twitter_img](https://camo.githubusercontent.com/25e3507bbe81f763f5792d81fbdb5838660ffe9dcc03235ce5ed4958844bea18/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769747465722d253430736872696b65682d626c75652e737667 "@shrikeh on Twitter")](https://twitter.com/shrikeh)

Trait-based helper library to take care of the heavy lifting of immutable collections in PHP.

Overview
--------

[](#overview)

This low-level library is just time-saving [traits](http://php.net/manual/en/language.oop5.traits.php "Link to traits overview in PHP 5.4") for creating immutable collections. As PHP 7 can use [return type hinting](http://php.net/manual/en/functions.returning-values.php "PHP 7 Returning Values"), I decided I would always return a Collection if there was a possibility of returning *0-n* objects from a method, and could then type hint the return value as a Collection, whether empty or not. PHP does not natively support immutable iterators, so I find that whenever I use [Domain-Driven Design](https://en.wikipedia.org/wiki/Domain-driven_design "Domain driven design definition"), and need an iterable list of [Value Objects](https://en.wikipedia.org/wiki/Value_object "Value Objects definition"), I have to do the same boilerplate re-work.

This pattern has been successful for me, as I can also strongly type the Collections themselves, so they can only contain objects of a given type. I also generally make them immutable, so they throw a descendent of [`DomainException`](http://php.net/manual/en/class.domainexception.php "PHP Domain Exception documentation") if you try to set or unset a value.

So as I use these more often, I split them up into Traits for re-use across my code. Feel free to use for yourself, they're tiny and just take care of boilerplate stuff for me.

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

[](#installation)

Recommended installation is via the ubiquitous and seminal [`composer`](https://getcomposer.org/ "Link to composer website"):

```
composer require --prefer-dist shrikeh/collections
```

Usage
-----

[](#usage)

The library consists of about a dozen traits that aid in matching core PHP and SPL interfaces such as [`ArrayAccess`](http://php.net/manual/en/class.arrayaccess.php "The ArrayAccess interface") and [`OuterIterator`](http://php.net/manual/en/class.outeriterator.php "The OuterIterator interface"). Generally I have an inner "storage", and ensure that access to this directly is removed, including mutable methods such as `offSetSet()` or `offsetUnset()`. This ensures that only the values added to the constructor can be iterated over.

As an example, to create a Collection that can only contain `SomeObject` objects:

```
