PHPackages                             moebrowne/erased-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. moebrowne/erased-generics

ActivePhp-ext[Utility &amp; Helpers](/categories/utility)

moebrowne/erased-generics
=========================

Erased generics support for PHP

0.1.0(3mo ago)10PHPPHP &gt;=8.1

Since Mar 31Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/moebrowne/erased-generics)[ Packagist](https://packagist.org/packages/moebrowne/erased-generics)[ RSS](/packages/moebrowne-erased-generics/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (2)Used By (0)

PHP Erased Generics Extension
=============================

[](#php-erased-generics-extension)

An experimental PHP extension which adds support for erased generics.

- Get an extension compiled and loaded
- Get a simple version working
- Write some docs
- Add some tests
- Add examples
- Support union types
- Support generic `T` and `TSomething` types
- Get it working with [PIE](https://github.com/php/pie)
- Test that it plays nicely with OPcache

Install
-------

[](#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
------------

[](#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
----------------

[](#supported-syntax)

### Functions

[](#functions)

```
// 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

[](#generic-type-parameters)

```
class Foo {
    public TModel $item;

    public function foo(TModel $value): TModel {}
}
```

### Classes

[](#classes)

```
// 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

[](#closures)

```
// 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

[](#nested-generics)

```
function foo(array $data) {}
function getNestedData(): array {}
```

### Unions

[](#unions)

```
// Union types with generics
function process(array|null $items) {}
function handle(Collection|array $data) {}
function fetch(): array|false {}

// Union types inside generic brackets
function process(Collection $data) {}
function transform(array $items) {}

// Union types in classes
class Container {
    public array|null $widgets;
    public Collection $items;

    public function __construct(
        public array|null $data,
    ) {}

    public function find(): array|null {}
}
```

### Fully Qualified Namespaces

[](#fully-qualified-namespaces)

```
function foo(array $items) {}
new Collection();
function getModels(): array {}
```

### Short-hand Nullable

[](#short-hand-nullable)

```
function foo(array
