grikdotnet/generics
Composer 安装命令:
composer require grikdotnet/generics
包简介
generics implementation in PHP
README 文档
README
- Story of generics in PHP
- Why do you need generics programming
- How to use this package
- Syntax
- System requirements and compatibility
- Solution diagrams
- Performance impact
Why do you need generics?
Generic programming is an algorithm where data types are declared as "to-be-specified-later", when needed.
It allows writing much less code, and have data types checked by the PHP engine in data sets.
Data may have unexpected structure, especially when it is obtained from databases, APIs, and 3rd party code. For single-value variables we define parameter types, but for the composite types such as array, ArrayObject, SplFixedArray one cannot define types of values in runtime. To define data types for values we could create multiple classes with the same code, where the only difference would be a type of a parameter. E.g.
class CollectionInt extends \ArrayObject{ public function offsetSet(int $key, int $value ) { parent::offsetSet($key,$value); } } class CollectionFoo extends \ArrayObject{ public function offsetSet(int $key, \Foo $value ) { parent::offsetSet($key,$value); } }
This feels wrong, and violates the "Don't repeat yourself" principle.
Generics allow defining types of parameters when you create an instance, with just one short clause. And yet you have just one class declaration for all types you need.
How to use
- Add the package as a dependency for Composer, as usually:
composer require grikdotnet\generics. - Call
new \grikdotnet\generics\Enable();in bootstrap to enable the class loader. - Declare a wildcard class.
#[\Generics\T] class Collection extends \ArrayObject{ use \grikdotnet\generics\GenericTrait; public function offsetSet(#[\Generics\T] $key, #[\Generics\T] $value ) { parent::offsetSet($key,$value); } }
Using the trait is optional. It provides a convenient shortcut method T() to create concrete types:
/** @var Collection $collection */ $collection = new (Collection::T("int","float"))(); $collection[] = 0.5;
That's it. Now PHP will check the type of values added to the ArrayObject instance, and trigger a TypeError when the type does not match.
Now let's use the typed Collection as a parameter type in a method:
class Model{ /** * @param Collection $numeric * @return int */ public function multiply(#[\Generics\T("Collection<int><float>")] $numeric): int { $result = 0; for ($numeric as $key => $value) { $value *= 2; } return $result; } }
This way data types of elements are checked by the PHP engine, and we can avoid writing a lot of type checks in a loop over data sets in every method.
Find more about syntax in the documentation.
grikdotnet/generics 适用场景与选型建议
grikdotnet/generics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 31, 最近一次更新时间为 2025 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 grikdotnet/generics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grikdotnet/generics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-18