时间:2019-07-25 12:28 编辑:
枚举
在数学和计算机科学理论中,一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数。这两种类型经常(但不总是)重叠。枚举是一个被命名的整型常数的集合,枚举在日常生活中很常见,例如表示星期的SUNDAY、MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY、SATURDAY就是一个枚举。—— 维基百科
枚举是一个被命名的整型常数的集合,枚举在日常生活中很常见,例如表示星期的SUNDAY、MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY、SATURDAY就是一个枚举。—— 维基百科
业务场景
在实际开发过程中我们非常容易接触到枚举类型,但是又因为 PHP 原生对枚举的支持不是太好,所以很多时候 开发人员并没有重视枚举的使用,而是使用全局常量或者类常量代替,而这两个数据原则上还是 字符串 并不能用来做类型判断。
字符串
订单状态 待支付/待发货/待收货/待评价
会员状态 激活/未激活
....
等等 ,很多时候我们都会用简单的 1/2/3/4 或者0/1 这样的方式去代表,然后在文档或者注释中规定这些东西。
更高级一点儿的就是定义成常量,然后方便统一存取,但是常量的值还是是字符串,无法进行类型判断。
这里就要看一下 PHP 对枚举的支持,虽然 PHP 对枚举没有完美的支持,但是在 SPL 中还是有一个基础的枚举类
1
2
3
4
5
6
7
SplEnum extends SplType {/ Constants /
SplEnum
extends
SplType {/ Constants /
const NULL __default = NULL ;
const
NULL __default = NULL ;
/ 方法 /
public getConstList ([ bool $include_default = FALSE ] ) : array
public
getConstList ([ bool
$include_default
= FALSE ] ) :
array
/ 继承的方法 /
SplType::__construct ( [mixed $initial_value [, bool $strict ]] )
SplType::__construct ( [mixed
$initial_value
[, bool
$strict
]] )
}
但是!这个需要额外的安装 PECL 用 PECL 安装 Spl_Types,无意间增加了使用成本,那有没有其他解决方案?答案是肯定的。
Spl_Types
直接手写一个。
首先定一个枚举
8
9
10
11
12
13
14
15
class Enum
class
Enum
{
// 默认值
const __default = self::WAIT_PAYMENT;
__default = self::WAIT_PAYMENT;
// 待付款
const WAIT_PAYMENT = 0;
WAIT_PAYMENT = 0;
// 待发货
const WAIT_SHIP = 1;
WAIT_SHIP = 1;
// 待收货
const WAIT_RECEIPT = 2;
WAIT_RECEIPT = 2;
<code class="php spaces" style="padding: 2px 4px; font-size: 13px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; margin: 0px !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; border: 0px !important; bottom: auto !important; flo
如何使用PHP来实现枚举?
时间:2019-07-25 12:28 编辑:
枚举
业务场景
在实际开发过程中我们非常容易接触到枚举类型,但是又因为 PHP 原生对枚举的支持不是太好,所以很多时候 开发人员并没有重视枚举的使用,而是使用全局常量或者类常量代替,而这两个数据原则上还是
字符串
并不能用来做类型判断。业务
订单状态 待支付/待发货/待收货/待评价
会员状态 激活/未激活
....
等等 ,很多时候我们都会用简单的 1/2/3/4 或者0/1 这样的方式去代表,然后在文档或者注释中规定这些东西。
更高级一点儿的就是定义成常量,然后方便统一存取,但是常量的值还是是字符串,无法进行类型判断。
这里就要看一下 PHP 对枚举的支持,虽然 PHP 对枚举没有完美的支持,但是在 SPL 中还是有一个基础的枚举类
SPL 枚举
1
2
3
4
5
6
7
SplEnum
extends
SplType {/ Constants /
const
NULL __default = NULL ;
/ 方法 /
public
getConstList ([ bool
$include_default
= FALSE ] ) :
array
/ 继承的方法 /
SplType::__construct ( [mixed
$initial_value
[, bool
$strict
]] )
}
但是!这个需要额外的安装 PECL 用 PECL 安装
Spl_Types
,无意间增加了使用成本,那有没有其他解决方案?答案是肯定的。直接手写一个。
开始准备
首先定一个枚举
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class
Enum
{
// 默认值
const
__default = self::WAIT_PAYMENT;
// 待付款
const
WAIT_PAYMENT = 0;
// 待发货
const
WAIT_SHIP = 1;
// 待收货
const
WAIT_RECEIPT = 2;
<code class="php spaces" style="padding: 2px 4px; font-size: 13px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; margin: 0px !important; background-image: none !important; background-position: initial !important; background-size: initial !important; background-repeat: initial !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; border: 0px !important; bottom: auto !important; flo