moebrowne/erased-generics

There is no license information available for the latest version (0.1.0) of this package.

Erased generics support for PHP

Maintainers

Package info

github.com/moebrowne/erased-generics

Type:php-ext

Ext name:ext-erased-generics

pkg:composer/moebrowne/erased-generics

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

0.1.0 2026-03-30 23:16 UTC

This package is auto-updated.

Last update: 2026-05-01 19:16:36 UTC


README

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
  • 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<Widget> becomes array.

Supported Syntax

Functions

// Function parameters
function foo(array<Widget> $items) {}
function foo(array<string, int> $map) {}
function foo(Map<string, Widget> $map) {}

// Return types
function getWidgets(): array<Widget> {}
function getMap(): Map<string, Widget> {}
function getData(): array<string, int> {}

Generic Type Parameters

class Foo<TModel> {
    public TModel $item;

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

Classes

// Class instantiation
new Thing<Widget>();
new Pair<string, int>();

// Class properties
class Foo {
    public array<Widget> $widgets;
}

// Class methods
class Foo {
    public function getWidgets(): array<Widget> {}
    private function getMap(): Map<string, int> {}
    protected static function getData(): Collection<Widget> {}
}

// Constructor promotion
class Foo {
    public function __construct(
        public Collection<Widget> $widgets,
        private array<int> $ids,
    ) {}
}

Closures

// Closure parameters
$process = function (array<Widget> $items) {};
$map = function (Map<string, int> $data) {};
$filter = fn (Collection<Widget> $widgets) => count($widgets);

// Closure return types
$closure = function(): array<Widget> {};
$fn = fn(): array<string, int> => [];

Nested Generics

function foo(array<Map<string, Widget>> $data) {}
function getNestedData(): array<Map<string, Widget>> {}

Unions

// Union types with generics
function process(array<Widget>|null $items) {}
function handle(Collection<Widget>|array<Widget> $data) {}
function fetch(): array<string, int>|false {}

// Union types inside generic brackets
function process(Collection<Widget|string> $data) {}
function transform(array<Widget|string|int> $items) {}

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

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

    public function find(): array<Widget>|null {}
}

Fully Qualified Namespaces

function foo(array<\App\Models\Widget> $items) {}
new Collection<\App\Models\Widget>();
function getModels(): array<\App\Models\Widget> {}

Short-hand Nullable

function foo(array<?Widget> $items) {}
function foo(?array<Widget> $items) {}