承接 moebrowne/erased-generics 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moebrowne/erased-generics

最新稳定版本:0.1.0

Composer 安装命令:

pie install moebrowne/erased-generics

包简介

Erased generics support for PHP

README 文档

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) {}

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2026-03-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固