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(1y ago)319MITPHPPHP &gt;=8.2

Since May 19Pushed 1y ago3 watchersCompare

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

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

33

—

LowBetter than 72% of packages

Maintenance46

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

409d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1154121?v=4)[Grigori Kochanov](/maintainers/grikdotnet)[@grikdotnet](https://github.com/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

[behat/behat

Scenario-oriented BDD framework for PHP

4.0k101.8M2.2k](/packages/behat-behat)[infection/infection

Infection is a Mutation Testing framework for PHP. The mutation adequacy score can be used to measure the effectiveness of a test set in terms of its ability to detect faults.

2.2k28.9M2.4k](/packages/infection-infection)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[brianhenryie/strauss

Prefixes dependencies namespaces so they are unique to your plugin

190438.1k37](/packages/brianhenryie-strauss)[phpdocumentor/reflection

Reflection library to do Static Analysis for PHP Projects

12525.9M149](/packages/phpdocumentor-reflection)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)

PHPackages © 2026

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