tebru/php-type
Composer 安装命令:
composer require tebru/php-type
包简介
A type wrapper for PHP that support generic syntax
README 文档
README
This library wraps a type string and provides an API around getting information about the type. It also supports generic type syntax.
General Usage
The simplest way is to instantiate a new TypeToken and pass in the type.
new TypeToken('string');
The class also normalizes the type:
$typeShort = new TypeToken('int'); $typeLong = new TypeToken('integer'); $typeShort->getRawType(); // 'integer' $typeLong->getRawType(); // 'integer' $typeShort->getPhpType(); // 'integer' $typeLong->getPhpType(); // 'integer' $typeShort->isInteger(); // true $typeLong->isInteger(); // true
Any of the core php types are supported as well as ? which represents
a wildcard type. This can be used if the type is unknown at the time
the type is instantiated. All of the possible types are represented
as constants on the class.
Classes also work the same
$type = new TypeToken(\My\Foo::class); $type->getRawType(); // 'My\Foo' (string)$type; // 'My\Foo' $type->getPhpType(); // 'object' $type->isObject(); // true $type->isA(\My\Foo::class); // true
->isA() checks the instantiated type's parent classes and interfaces
in addition to the passed in class name.
You can also use generic syntax with angle brackets.
$type = new TypeToken('My\Foo<string, My\Foo2>'); $type->getRawType(); // 'My\Foo' (string)$type; // 'My\Foo<string, My\Foo2>' $type->getPhpType(); // 'object' $type->isObject(); // true $type->isA(\My\Foo::class); // true $generics = $type->getGenerics(); (string)$generics[0]; // 'string' (string)$generics[1]; // 'My\Foo2'
Calling ->getGenerics() will return an array of TypeToken objects.
Nested generics work the same way
new TypeToken('array<string, array<int>>');
This could represent an array with string keys and all values are an array of integers.
If you have a variable, you can get the type using the static factory method
TypeToken::createFromVariable($variable);
This uses the singleton method ::create() which will return the same instance on duplicate types.
tebru/php-type 适用场景与选型建议
tebru/php-type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 192.35k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tebru/php-type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tebru/php-type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 192.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-18