Download the PHP package stratadox/immutable-collection without Composer
On this page you can find all versions of the php package stratadox/immutable-collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package immutable-collection
ImmutableCollection
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
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
Install using composer:
Basic usage
The most basic implementation of the immutable collection might look something like the following:
Or:
Advanced usage
Additional behaviour can be added by implementing the relevant interfaces and/or traits for the particular concrete collection:
Multiple kinds of behaviour can be used simultaneously:
The shorthand for which is:
If desired, the methods current
and offsetGet
may be implemented with a
return type hint, in order to (further) increase type safety and to allow
IDE's like PhpStorm to provide code completion in loops and array-like access:
All versions of immutable-collection with dependencies
stratadox/collection-contracts Version ^5.0
stratadox/specification-interfaces Version ^1.0