gzoran / constant
常量类提供了一些对常量操作的基本方法
1.1.1
2019-03-26 08:39 UTC
Requires (Dev)
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2025-03-27 05:34:51 UTC
README
常量抽象类 ConstantAbstract 实现了 ConstantContract 契约,提供对常量便捷定义与访问等的操作
安装
composer require "gzoran/constant:~1.0"
使用
现在我们建立一个 “游戏状态” 常量类 GameStatus。
class GameStatus extends ConstantAbstract { /** * 定义常量 禁用 * * @var int */ const DISABLE = 0; /** * 定义常量 激活 */ const ACTIVE = 1; /** * 这里必须实现常量契约的 mapping 方法,用以配置 “常量” 与 “名称” 的对应关系 * * @author Mike * @return array */ public static function mapping() { return [ self::DISABLE => '已禁用', self::ACTIVE => '已激活' ]; } }
使用常量
// 如果游戏状态为激活,则返回 true if ($status == GameStatus::ACTIVE) { return true; }
获取常量名称
$status = 1; // 获取状态为 1 的名称 $description = GameStatus::name($status);
检查常量是否配置
$status = 1; // 如果状态 1 已配置,则返回 true if (GameStatus::isExist($status)) { return true; }
License
MIT