moebrowne/erased-generics
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
TandTSomethingtypes - 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) {}
moebrowne/erased-generics 适用场景与选型建议
moebrowne/erased-generics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 moebrowne/erased-generics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 moebrowne/erased-generics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2026-03-30