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

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

xp-lang/xp-generics
===================

XP generics for PHP

v2.1.0(1y ago)12.3k↓50%[1 issues](https://github.com/xp-lang/xp-generics/issues)BSD-3-ClausePHPPHP &gt;=7.4.0

Since Nov 6Pushed 1y ago2 watchersCompare

[ Source](https://github.com/xp-lang/xp-generics)[ Packagist](https://packagist.org/packages/xp-lang/xp-generics)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-lang-xp-generics/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (15)Used By (0)

XP generics for PHP
===================

[](#xp-generics-for-php)

[![Build status on GitHub](https://github.com/xp-lang/xp-generics/workflows/Tests/badge.svg)](https://github.com/xp-lang/xp-generics/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/59d0d7fe169172c4184020565a25c2585c8ce5102b0f27480531079331159304/68747470733a2f2f706f7365722e707567782e6f72672f78702d6c616e672f78702d67656e65726963732f76657273696f6e2e737667)](https://packagist.org/packages/xp-lang/xp-generics)

Plugin for the [XP Compiler](https://github.com/xp-framework/compiler/) which adds support for XP generics.

Example
-------

[](#example)

```
// Declaration
namespace com\example;

class PriorityQueue {
  private $elements;
  private $comparator= null;
  private $sorted= true;

  public function __construct(E... $elements) {
    $this->elements= $elements;
  }

  public function comparing(?function(E, E): int $comparator): self {
    $this->comparator= $comparator;
    return $this;
  }

  public function push(E $element): void {
    $this->elements[]= $element;
    $this->sorted= false;
  }

  public function pop(): ?E {
    if (!$this->sorted) {
      $this->comparator ? usort($this->elements, $this->comparator) : sort($this->elements);
      $this->sorted= true;
    }
    return array_pop($this->elements);
  }
}

// Usage
$q= new PriorityQueue();
$q->push('Test');

$q->push(123); // lang.IllegalArgumentException
```

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

[](#installation)

After installing the XP Compiler into your project, also include this plugin.

```
$ composer require xp-framework/compiler
# ...

$ composer require xp-lang/xp-generics
# ...
```

No further action is required.

See also
--------

[](#see-also)

- [Generics in PHP??? - PHP Annotated](https://www.youtube.com/watch?v=ffhhx5_TUB8) from August 2024
- [State of Generics and Collections](https://thephp.foundation/blog/2024/08/19/state-of-generics-and-collections/) from August 2024
- [XP RFC: Generics](https://github.com/xp-framework/rfc/issues/106) from January 2007
- [XP RFC: Generics optimization](https://github.com/xp-framework/rfc/issues/193)
- [PHP RFC: Generics](https://wiki.php.net/rfc/generics)
- [HHVM Generics](https://docs.hhvm.com/hack/generics/introduction)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance14

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

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 ~49 days

Recently: every ~77 days

Total

14

Last Release

652d ago

Major Versions

v0.7.0 → v1.0.02024-03-02

v1.0.0 → v2.0.02024-03-24

PHP version history (2 changes)v0.1.0PHP &gt;=7.0.0

v2.0.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (77 commits)")

---

Tags

compiler-plugingeneric-typesgenericsphp7php8xp-compilerxp-frameworklanguagemodulexp

### Embed Badge

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

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

PHPackages © 2026

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