PHPackages                             serens/typed-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. serens/typed-collection

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

serens/typed-collection
=======================

A basic library to support typed collections

1.0.1(4mo ago)01proprietaryPHPPHP &gt;=8.3

Since Dec 14Pushed 4mo ago1 watchersCompare

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

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

Typed Collection
================

[](#typed-collection)

Typed Collection is an attempt to use the concept of “Generics” from other programming languages in PHP as well. Unfortunately, not all advantages can be realized with current PHP on-board means and syntax.

Usage and examples
------------------

[](#usage-and-examples)

```
class A {}
class B extends A {}

// Creata a collection which can only contain instances of A.
// Sub classes of A and other classes *are not allowed*:
$collection = new \Serens\TypedCollection\ClassCollection(A::class);
$collection[] = new \A(); // ok
$collection[] = new \B(); // will fail (InvalidArgumentException)

// Creata a collection which can only contain instances of \Iterable.
// Sub classes *are allowed*:
$collection = new \Serens\TypedCollection\InstanceCollection(\Iterator::class);
$collection[] = new \EmptyIterator(); // ok

// Create a collection which can only contain strings:
$collection = new \Serens\TypedCollection\ScalarCollection(\Serens\TypedCollection\ScalarType::STRING);
$collection[] = 'A string can be added to this collection.'; // ok
$collection[] = 324; // will fail (InvalidArgumentException)

// The constructor also accepts initialization values:
$collection = new \Serens\TypedCollection\ScalarCollection(
    \Serens\TypedCollection\ScalarType::STRING,
    [ 'A string', 'Another string', 'The last string' ]
);
```

ClassCollection vs. InstanceCollection
--------------------------------------

[](#classcollection-vs-instancecollection)

Both collections work very similarly. The difference is that the `ClassCollection` can only hold instances of a specific, exact class. The `InstanceCollection`, on the other hand, can also include instances of derived classes of the specified parent class. The `InstanceCollection` is more suitable for interfaces.

Notes
-----

[](#notes)

A major disadvantage is that static code analysis or even the type hinting of the IDE know nothing about the configured types of a collection. Elements of type 'mixed' are always recognized for them.

If you absolutely want to use type hinting, you can derive a typed collection and overwrite all relevant methods and determine the type using PHPDoc. However, this approach would generate a lot of code that would mainly consist of parent calls and PHPDocs.

###  Health Score

36

—

LowBetter than 81% of packages

Maintenance78

Regular maintenance activity

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

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

146d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/48594421e6c4348d840049f0bf0b2b3022d0737d9630a647a8ca62efbf667715?d=identicon)[serens](/maintainers/serens)

---

Top Contributors

[![serens](https://avatars.githubusercontent.com/u/4192751?v=4)](https://github.com/serens "serens (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/serens-typed-collection/health.svg)

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

###  Alternatives

[spatie/unit-conversions

Perform unit conversions in PHP

363.5k](/packages/spatie-unit-conversions)[devaly/wordpress-routes

Manage rewrites and routes in WordPress with this dead-simple plugin

101.2k](/packages/devaly-wordpress-routes)

PHPackages © 2026

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