vcn/enum
最新稳定版本:v1.5.2
Composer 安装命令:
composer require vcn/enum
包简介
[Deprecated] Enumeration data types with pattern match operations. This package is obsolete because PHP has native enums now.
README 文档
README
Enumeration data types with pattern match operations.
Quickstart
composer require vcn/enum
<?php use Vcn\Lib\Enum; /** * @method static Fruit APPLE() * @method static Fruit BANANA() */ class Fruit extends Enum { protected const APPLE = 0; protected const BANANA = 0; } Fruit::APPLE()->equals(Fruit::BANANA()); // false Fruit::APPLE()->equals(Fruit::APPLE()); // true Fruit::APPLE() ->when(Fruit::APPLE(), 'apple') ->when(Fruit::BANANA(), 'banana') ->get(); // 'apple'
Motivation
An enum (or enumeration), is a data construct describing a value that can be exactly one of a distinct, predefined set of values.
We could for instance model fruit - where we only consider apples and bananas as fruit - as follows (using ADTs):
data Fruit = Apple | Banana
A fruit is either an apple or banana - not both, nor neither.
Here Fruit is the enumerable type and Apple and Banana are its labels (its possible values, or instances).
This implementation tries to come close to the expressive power above.
Usage
data Fruit = Apple | Banana
To express the above using this implementation, firstly extend Enum to define Fruit:
<?php final class Fruit extends Enum {}
(Making the class final is recommended, as further inheritance is not supported and will produce warnings.)
Then define the labels as constant members of Fruit:
<?php final class Fruit extends Enum { protected const APPLE = 0; protected const BANANA = 0; }
(The constant values (0s) are meaningless, but required by PHP.)
This will expose the labels as magic static methods Fruit::APPLE() and Fruit::BANANA(). They serve as the constructors of the corresponding labels. It is recommended to annotate them as class members in the docblock:
<?php /** * @method static Fruit APPLE() * @method static Fruit BANANA() */ final class Fruit extends Enum { protected const APPLE = 0; protected const BANANA = 0; }
Now you can instantiate either label:
<?php $banana = Fruit::APPLE(); $apple = Fruit::BANANA();
You can test for equality:
<?php Fruit::APPLE()->equals(Fruit::BANANA()); // false Fruit::APPLE()->equals(Fruit::APPLE()); // true
You can pattern match on labels:
<?php Fruit::APPLE() ->when(Fruit::APPLE(), 'apple') ->when(Fruit::BANANA(), 'banana') ->get(); // 'apple'
You can stringify and unstringify label names:
<?php Fruit::APPLE()->getName(); // 'APPLE' Fruit::byName('APPLE'); // Fruit::APPLE()
You can check a collection for exhaustiveness:
<?php Fruit::isExhaustive([Fruit::APPLE()]); // false Fruit::isExhaustive([ Fruit::APPLE(), Fruit::BANANA() ]); // true
vcn/enum 适用场景与选型建议
vcn/enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.89k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 vcn/enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vcn/enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 29.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04