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

ActiveLibrary

krlove/collections
==================

Strictly typed data structures for PHP

1.0.0(3y ago)031MITPHPPHP &gt;=7.4

Since Aug 14Pushed 3y ago1 watchersCompare

[ Source](https://github.com/krlove/collections)[ Packagist](https://packagist.org/packages/krlove/collections)[ RSS](/packages/krlove-collections/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Collections
===========

[](#collections)

**Strictly typed** data structures for PHP.

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

[](#installation)

```
composer require krlove/collections

```

Sequence
--------

[](#sequence)

Sequence is an ordered collection of variables of any type.

```
$sequence = Sequence::of('string');
$sequence->push('Gandalf');
$sequence->push('Bilbo');
$sequence->push('Frodo');
$sequence->remove(1);

foreach ($sequence as $index => $value) {
    echo $index . ': ' . $value . PHP_EOL;
}
```

```
0: Gandalf
1: Frodo

```

Map
---

[](#map)

Map contains key-value pairs, where each key is unique.

```
$map = Map::of('string', 'array');
$map->set('fruits', ['apple', 'banana', 'pear']);
$map->set('vegetables', ['tomato', 'potato', 'onion']);
$map->set('berries', ['strawberry', 'blueberry', 'raspberry']);
$map->remove('vegetables');
$map->set('berries', ['bilberry']);

foreach ($map as $key => $value) {
    echo $key . ': ' . var_export($value, true) . PHP_EOL;
}
```

```
fruits: array (
  0 => 'apple',
  1 => 'banana',
  2 => 'pear',
)
berries: array (
  0 => 'bilberry',
)

```

Set
---

[](#set)

Set is a collection of unique variables of any type.

```
$set = Set::of('string');
$set->add('Gandalf');
$set->add('Bilbo');
$set->add('Bilbo');

echo var_export($set->toArray(), true) . PHP_EOL;
```

```
array (
  0 => 'Gandalf',
  1 => 'Bilbo',
)

```

Types
-----

[](#types)

All collections are strictly typed.

```
$sequence = Sequence::of('int');
$sequence->push('Gandalf');
```

```
PHP Fatal error:  Uncaught Krlove\Collection\Exception\TypeException: Variable must be of type int, string given

```

Supported types are:

- null
- bool
- int
- float
- string
- array
- iterable
- callable
- resource
- object
- class (objects of specific class or interface)
- mixed (any type is allowed)

Nullable
--------

[](#nullable)

Types can be nullable.

```
$sequence = Sequence::of('?string');
$sequence->push(null);
```

Freezable
---------

[](#freezable)

After collection is "frozen", it becomes read-only, no changes are allowed to it. It is impossible to "unfreeze" the collection once it is frozen, but it is possible to **copy** it.

```
$sequence = Sequence::of('string');
$sequence->push('Gandalf');
$sequence->freeze();
//$sequence->push('Bilbo'); Fatal error: Uncaught Krlove\Collection\Exception\FrozenException: Sequence is frozen and can not be changed
$copy = $sequence->copy();
$copy->push('Bilbo');

foreach ($copy as $index => $value) {
    echo $index . ': ' . $value . PHP_EOL;
}
```

```
0: Gandalf
1: Bilbo

```

Usage
-----

[](#usage)

PHP does not support generic types, so it is impossible to define a property as follows.

```
private Map $map; // invalid
```

Some additional code must be written to ensure collections of proper types are used

```
class MyClass
{
    /**
     * @var Map
     */
    private Map $map;

    public function __construct()
    {
        $this->map = Map::of('int', 'string');
    }

    /**
     * @param Map $map
     * @return void
     */
    public function setMap(Map $map): void
    {
        if (!$map->isOf('int', 'string')) {
            // throw exception
        }

        $this->map = $map;
    }
}
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1372d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d22d5c2b3d26f1b5d545c2f83dcac6aa88f5376cac8df537fd0c628eca0dcfa?d=identicon)[krlove](/maintainers/krlove)

---

Top Contributors

[![krlove](https://avatars.githubusercontent.com/u/1562405?v=4)](https://github.com/krlove "krlove (82 commits)")

---

Tags

phpcollectionsstrict types

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/krlove-collections/health.svg)

```
[![Health](https://phpackages.com/badges/krlove-collections/health.svg)](https://phpackages.com/packages/krlove-collections)
```

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k411.1M1.2k](/packages/doctrine-collections)[cartalyst/collections

Collection Abstaction library for PHP.

76908.6k4](/packages/cartalyst-collections)[smartemailing/types

Lightweight collection of handy PHP value objects

9368.7k1](/packages/smartemailing-types)[strictus/strictus

Strict Typing for local variables in PHP

1606.9k](/packages/strictus-strictus)[webparking/laravel-type-safe-collection

This package provides type-safe extension of the laravel collection, forcing a single type of object.

378.2k](/packages/webparking-laravel-type-safe-collection)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
