aldemeery / enum-polyfill
PHP 中 SplEnum 类型的 Polyfill
v1.0.1
2020-12-23 01:51 UTC
Requires
- php: >=7.1
This package is auto-updated.
Last update: 2024-09-23 10:12:41 UTC
README
这是一个 PECL 扩展 SPL_Types 中 SplEnum
类型的 Polyfill,因此您不需要在您的服务器上下载和安装扩展。
SplEnum 允许在 PHP 中原生地模拟和创建枚举对象。
示例
来自 php.net 文档。
<?php class Month extends \SplEnum { const __default = self::January; const January = 1; const February = 2; const March = 3; const April = 4; const May = 5; const June = 6; const July = 7; const August = 8; const September = 9; const October = 10; const November = 11; const December = 12; } echo new Month(Month::June) . PHP_EOL; try { new Month(13); } catch (\UnexpectedValueException $uve) { echo $uve->getMessage() . PHP_EOL; } ?>
上面的示例将输出
6
Value '13' is not part of the enum Month
类概要
abstract class SplEnum { /* Constants */ const NULL __default = NULL ; /* Methods */ SplType::__construct ([ mixed $initial_value ] ) public array getConstList ([ bool $include_default = FALSE ] ) }
预定义常量
/* The default value of the enum */ SplEnum::__default
方法
SplType::__construct
SplEnum
构造函数。
SplType::__construct ([ mixed $initial_value ] )
参数
initial_value
枚举的初始值。
错误/异常
如果给出不兼容的类型,则抛出 UnexpectedValueException
。
SplEnum::getConstList
返回所有常量作为数组
public array SplEnum::getConstList ([ bool $include_default = false ] )
参数
include_default
是否包含 __default 属性。
作者
- Osama Aldemeery aldemeery@gmail.com