PHPackages                             grikdotnet/generics - 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. grikdotnet/generics

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

grikdotnet/generics
===================

generics implementation in PHP

1.0.0(12mo ago)319MITPHPPHP &gt;=8.2

Since May 19Pushed 12mo ago3 watchersCompare

[ Source](https://github.com/grikdotnet/generics)[ Packagist](https://packagist.org/packages/grikdotnet/generics)[ RSS](/packages/grikdotnet-generics/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (6)Used By (0)

Generics in PHP
===============

[](#generics-in-php)

- [Story of generics in PHP](documentation/story.md)
- [Why do you need generics programming](#why-do-you-need-generics)
- [How to use this package](#how-to-use)
- [Syntax](documentation/syntax.md)
- [System requirements and compatibility](documentation/compatibility.md)
- [Solution diagrams](documentation/implementation.md)
- [Performance impact](documentation/performance.md)

### Why do you need generics?

[](#why-do-you-need-generics)

[Generic programming](https://en.wikipedia.org/wiki/Generic_programming) is an algorithm where data types are declared as "to-be-specified-later", when needed.

It allows writing much less code, and have data types checked by the PHP engine in data sets.

Data may have unexpected structure, especially when it is obtained from databases, APIs, and 3rd party code. For single-value variables we define parameter types, but for the composite types such as array, ArrayObject, SplFixedArray one cannot define types of values in runtime. To define data types for values we could create multiple classes with the same code, where the only difference would be a type of a parameter. E.g.

```
class CollectionInt extends \ArrayObject{
    public function offsetSet(int $key, int $value )
    {
        parent::offsetSet($key,$value);
    }
}
class CollectionFoo extends \ArrayObject{
    public function offsetSet(int $key, \Foo $value )
    {
        parent::offsetSet($key,$value);
    }
}
```

This feels wrong, and violates the "Don't repeat yourself" principle.

Generics allow defining types of parameters when you create an instance, with just one short clause. And yet you have just one class declaration for all types you need.

### How to use

[](#how-to-use)

1. Add the package as a dependency for Composer, as usually: `composer require grikdotnet\generics`.
2. Call `new \grikdotnet\generics\Enable();` in bootstrap to enable the class loader.
3. Declare a wildcard class.

```
#[\Generics\T]
class Collection extends \ArrayObject{
    use \grikdotnet\generics\GenericTrait;

    public function offsetSet(#[\Generics\T] $key, #[\Generics\T] $value )
    {
        parent::offsetSet($key,$value);
    }
}
```

Using the trait is optional. It provides a convenient shortcut method `T()` to create concrete types:

```
/** @var Collection $collection */
$collection = new (Collection::T("int","float"))();
$collection[] = 0.5;
```

That's it. Now PHP will check the type of values added to the ArrayObject instance, and trigger a TypeError when the type does not match.

Now let's use the typed Collection as a parameter type in a method:

```
class Model{
    /**
    * @param Collection $numeric
    * @return int
    */
    public function multiply(#[\Generics\T("Collection")] $numeric): int
    {
        $result = 0;
        for ($numeric as $key => $value) {
            $value *= 2;
        }
        return $result;
    }
}
```

This way data types of elements are checked by the PHP engine, and we can avoid writing a lot of type checks in a loop over data sets in every method.

Find more about syntax in the [documentation](documentation/syntax.md).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance50

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity53

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

Every ~0 days

Total

5

Last Release

361d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2fed4bdab3645292e4fa5e0bd12054f33e15c82b059bccd06b711be009f76d55?d=identicon)[grikdotnet](/maintainers/grikdotnet)

---

Top Contributors

[![grikdotnet](https://avatars.githubusercontent.com/u/1154121?v=4)](https://github.com/grikdotnet "grikdotnet (37 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/grikdotnet-generics/health.svg)

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

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M568](/packages/symfony-maker-bundle)[icanhazstring/composer-unused

Show unused packages by scanning your code

1.7k7.0M188](/packages/icanhazstring-composer-unused)[roave/backward-compatibility-check

Tool to compare two revisions of a public API to check for BC breaks

5953.3M56](/packages/roave-backward-compatibility-check)[pocketmine/pocketmine-mp

A server software for Minecraft: Bedrock Edition written in PHP

3.5k74.6k86](/packages/pocketmine-pocketmine-mp)[coenjacobs/mozart

Composes all dependencies as a package inside a WordPress plugin

4723.6M20](/packages/coenjacobs-mozart)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)

PHPackages © 2026

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