chrisharrison/php-array-of
Composer 安装命令:
composer require chrisharrison/php-array-of
包简介
Implement an array of a defined type. Generics replacement for PHP.
README 文档
README
Implement an array of a defined type. Generics replacement for PHP.
Requirements
Requires PHP >= 7.1
Installation
Through Composer, obviously:
composer require chrisharrison/php-array-of
Why?
PHP 7 has pretty good support for type declarations, both in arguments and returns. It now also handles primitives as well as class names. For example:
public function dealCard(string $cardName): Card;
One thing is still lacking. Array generics. There's an RFC for implementing 'Array Of'. It's been around for quite a while and was roundly rejected.
This library is a workaround for that. It allows you to make type declarations for arrays of a particular type.
Usage
Using an existing implementation
The library comes with ArrayOf implementations for all of the PHP scalar types. i.e.:
ArrayOfIntegerArrayOfFloatArrayOfStringArrayOfBoolean
These can then be used in a type declaration:
public function getIntegers(): ArrayOfInteger;
An ArrayOfInteger can be created:
$integers = new ArrayOfInteger([1,1,2,3,5,8,13]);
and used like an array:
$sum = $integers[5] + $integers[6]; // equals 21
Implementing your own type
You can create your own ArrayOfs for your own types.
final class ArrayOfCard extends ArrayOf { protected function typeToEnforce(): string { return Card::class; } }
An ArrayOfCard can be created thus:
$aceOfSpades = new Card('spades', 'ace'); $threeOfClubs = new Card('clubs', '3'); $cards = new ArrayOfCard([$aceOfSpades, $threeOfClubs]);
Other concerns
Enforcement
Members of an ArrayOf are enforced as being of the type specified in the typeToEnforce abstract method. This enforcement occurs on instantiation at runtime. If you try to instantiate with a member of a non-matching type, an exception will be thrown.
Permissible types
Only PHP scalars and objects can be members of an ArrayOf. So no callables and no arrays.
Immutability
ArrayOfs are mutable. This library provides the capability to define immutable objects in the form of ImmutableArrayOfs. These objects extend from ArrayOf and work in the same way except after the initial instantiation, no further changes can be made to the object. If you try to perform a write operation (e.g. unset) on it, an exception will be thrown. All of the scalar types are also provided in immutable form:
ImmutableArrayOfIntegerImmutableArrayOfFloatImmutableArrayOfStringImmutableArrayOfBoolean
chrisharrison/php-array-of 适用场景与选型建议
chrisharrison/php-array-of 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 783.36k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2017 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 chrisharrison/php-array-of 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chrisharrison/php-array-of 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 783.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-11