# PHP Erased Generics Extension
An experimental PHP extension which adds support for erased generics.
- [x] Get an extension compiled and loaded
- [x] Get a simple version working
- [x] Write some docs
- [x] Add some tests
- [x] Add examples
- [x] Support union types
- [x] Support generic `T` and `TSomething` types
- [x] Get it working with [PIE](https://github.com/php/pie)
- [ ] Test that it plays nicely with OPcache
## Install
```
pie install moebrowne/erased-generics
```
Compile Manually
```
make clean
phpize --clean
phpize
./configure
make
sudo make install
```
Add `extension=erased_generics.so` to your php.ini or create a new ini file (the location is OS dependant).
The extension can be loaded manually on the CLI:
```
php -d extension=/path/to/erased-generics/modules/erased_generics.so -f file.php
```
## How It Works
It's a really simple transpiler, kind of like Typescript or SCSS but purely subtractive. Unlike TS and SCSS, everything
is done at runtime. There is no additional compilation or tools the developer must run. A slightly more technical
explanation: It overrides the `zend_compile_file` function (responsible for reading and compiling PHP code)
with simple string parsing stripping out the generics syntax transparently hiding the generics syntax from the rest of
the compilation process.
Native type declarations are kept where possible, for example `array` becomes `array`.
## Supported Syntax
### Functions
```php
// Function parameters
function foo(array $items) {}
function foo(array $map) {}
function foo(Map $map) {}
// Return types
function getWidgets(): array {}
function getMap(): Map {}
function getData(): array {}
```
### Generic Type Parameters
```php
class Foo {
public TModel $item;
public function foo(TModel $value): TModel {}
}
```
### Classes
```php
// Class instantiation
new Thing();
new Pair();
// Class properties
class Foo {
public array $widgets;
}
// Class methods
class Foo {
public function getWidgets(): array {}
private function getMap(): Map {}
protected static function getData(): Collection {}
}
// Constructor promotion
class Foo {
public function __construct(
public Collection $widgets,
private array $ids,
) {}
}
```
### Closures
```php
// Closure parameters
$process = function (array $items) {};
$map = function (Map $data) {};
$filter = fn (Collection $widgets) => count($widgets);
// Closure return types
$closure = function(): array {};
$fn = fn(): array => [];
```
### Nested Generics
```php
function foo(array