https://github.com/xp-lang/xp-generics
XP generics for PHP
https://github.com/xp-lang/xp-generics
compiler-plugin generic-types generics php7 php8 xp-compiler xp-framework
Last synced: 3 months ago
JSON representation
XP generics for PHP
- Host: GitHub
- URL: https://github.com/xp-lang/xp-generics
- Owner: xp-lang
- Created: 2022-11-06T13:19:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-01T08:07:59.000Z (10 months ago)
- Last Synced: 2025-03-02T06:42:56.770Z (4 months ago)
- Topics: compiler-plugin, generic-types, generics, php7, php8, xp-compiler, xp-framework
- Language: PHP
- Homepage:
- Size: 73.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
Awesome Lists containing this project
README
XP generics for PHP
===================[](https://github.com/xp-lang/xp-generics/actions)
[](https://github.com/xp-framework/core)
[](https://github.com/xp-framework/core/blob/master/LICENCE.md)
[](http://php.net/)
[](http://php.net/)
[](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
-------```php
// 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
------------
After installing the XP Compiler into your project, also include this plugin.```bash
$ composer require xp-framework/compiler
# ...$ composer require xp-lang/xp-generics
# ...
```No further action is required.
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)