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

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

aziule/typed-collections
========================

Adding some typing in arrays or object collections

v1.0.0(8y ago)118MITPHPPHP &gt;=5.6

Since May 31Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Aziule/typed-collections)[ Packagist](https://packagist.org/packages/aziule/typed-collections)[ RSS](/packages/aziule-typed-collections/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

Typed Collections
=================

[](#typed-collections)

> ## TL;DR
>
> [](#tldr)
>
> typed-collections is a project aiming to sensibilise developers to write good code and documentation when it comes to manipulating arrays in PHP.
>
> Jump to the [installation](#installation) or [documentation](#documentation)

PHP's dynamically typed nature can lead bad developers to write abominable code. This is especially true for collections (arrays) of items, where one can only rely on the PHPDoc (if any, up-to-date and accurate) or on a variable's name to guess what the array actually contains:

**Absolute worst case:**

```
public function ($elements); // Too vague
```

**Bad - and unfortunately, too common:**

```
/**
* @param array $ips
*/
public function (array $ips); // Still too vague (can be associative, contain objects, etc.)
```

**Common practice**

```
/**
* @param MyObject[] $objects
*/
public function (array $objects); // Best practice
```

Installation
============

[](#installation)

Install it via [Composer](https://getcomposer.org):

```
composer require aziule/typed-collections

```

Documentation
=============

[](#documentation)

Supported types
---------------

[](#supported-types)

The library supports most of PHP's primitive types, as well as user-defined objects collections:

- Array
- Boolean
- Double
- Int
- String
- User-defined Object

Usage
-----

[](#usage)

### For primitive types

[](#for-primitive-types)

Primitive type collections will allow to store only items of this type (only ints, only booleans, etc.).

Here is the list of available primitive collection classes:

```
Aziule\TypedCollections\ArrayCollection;
Aziule\TypedCollections\BooleanCollection;
Aziule\TypedCollections\DoubleCollection;
Aziule\TypedCollections\IntCollection;
Aziule\TypedCollections\StringCollection;
```

**Empty collection**

```
use Aziule\TypedCollections\IntCollection;

$collection = new IntCollection(); // Empty at that stage
$collection[] = 42;

foreach ($collection as $item) {
    // ...
}
```

**Pre-filled collection**

```
use Aziule\TypedCollections\StringCollection;

$collection = new StringCollection([
    'my',
    'collection',
    'of',
    'strings',
]);

echo count($collection); // 4
```

### For user-defined objects

[](#for-user-defined-objects)

Two different methods exist for creating and passing user-defined objects collections:

- [Using `ObjectCollection`](#using-objectcollection)
- [Creating a custom collection extending `ObjectCollection`](#creating-a-custom-collection)

#### Using `ObjectCollection`

[](#using-objectcollection)

**Example**

```
use Aziule\TypedCollections\ObjectCollection;

class MyObject {
    // ...
}

$collection = new ObjectCollection(MyObject::class); // The collection is of type MyObject
$collection[] = new MyObject();
$collection[] = new \DateTime(); // Will throw an InvalidItemTypeException
```

Just pass an array of objects as a second argument of the `ObjectCollection` constructor to initialise it:

```
use Aziule\TypedCollections\ObjectCollection;

class MyObject {
    // ...
}

$collection = new ObjectCollection(MyObject::class, [
    new MyObject(),
    new MyObject(),
]);

echo count($collection); // 2
```

If you need to know what kind of object are embedded in the `ObjectCollection`:

```
use Aziule\TypedCollections\ObjectCollection;

$collection = new ObjectCollection(\stdClass::class);
echo $collection->getClass(); // 'stdClass'
```

#### Creating a custom collection

[](#creating-a-custom-collection)

If you want to pass an even more strongly-typed collection, you can create a custom collection for each type of object you need.

**Example:**

```
use \Aziule\TypedCollections\ObjectCollection;

class Item
{
    private $value;

    public function __construct($value)
    {
        $this->value = $value;
    }
}

class ItemCollection extends ObjectCollection
{
    public function __construct(array $items = [])
    {
        parent::__construct(Item::class, $items);
    }
}

$collection = new ItemCollection();
$collection[] = new Item('Foo');
$collection[] = new Item('Bar');

// And then use this collection
public function doSomething(ItemCollection $itemCollection)
{
    // ...
}
```

Test
----

[](#test)

```
./vendor/bin/phpunit
```

###  Health Score

26

—

LowBetter than 46% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

3217d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b17d1101a6ced5914a1d3093c441e3735f89ccd3d52aaa185ad76a8caf60093?d=identicon)[Aziule](/maintainers/Aziule)

---

Top Contributors

[![aziule](https://avatars.githubusercontent.com/u/1004323?v=4)](https://github.com/aziule "aziule (31 commits)")

---

Tags

arraycollectiontyping

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar to jQuery and Laravel Collections

4.2k399.2k6](/packages/aimeos-map)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4511.2M5](/packages/athari-yalinqo)[yansongda/supports

common components

211.3M29](/packages/yansongda-supports)[armincms/json

A Laravel Nova field.

25146.9k3](/packages/armincms-json)[graze/sort

A collection of array sorting transforms and functions

12287.2k2](/packages/graze-sort)[graze/data-structure

Data collections and containers

12285.1k8](/packages/graze-data-structure)

PHPackages © 2026

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