Plugin for the XP Compiler which adds support for XP generics.
// Declaration
namespace com\example;
class PriorityQueue<E> {
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;
$this->sorted= !empty($this->elements);
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<string>();
$q->push('Test');
$q->push(123); // lang.IllegalArgumentExceptionAfter 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.
- XP Core: Generic Methods available in XP 12.13+
- Generics: The Comments Become Code by Holly Schilling from July 2026
- PHP RFC: Bound-Erased Generic Types from May 2026 - declined
- PHP generics written in PHP from Anton Sukhachev
- Generics in PHP??? - PHP Annotated from August 2024
- State of Generics and Collections from August 2024
- XP RFC: Generics from January 2007
- XP RFC: Generics optimization
- PHP RFC: Generics
- HHVM Generics

