当前位置: 首页 > news >正文

PHP 使用常量实现枚举类

PHP 使用常量实现枚举类

<?php
abstract class Enum {private static $constCacheArray = NULL;private static function getConstants() {if (self::$constCacheArray == NULL) {self::$constCacheArray = [];}$calledClass = get_called_class();if (!array_key_exists($calledClass, self::$constCacheArray)) {$reflect = new ReflectionClass($calledClass);self::$constCacheArray[$calledClass] = $reflect->getConstants();}return self::$constCacheArray[$calledClass];}public static function getEnumValue($constant) {$constants = self::getConstants();return $constants[$constant] ?? null;}
}class StatusCode extends Enum {const OK = 200;const NOT_FOUND = 404;// ... 其他状态码
}// 使用类常量直接访问枚举值
$statusCode = StatusCode::getEnumValue(StatusCode::OK);
echo $statusCode; // 输出 200
http://www.lryc.cn/news/348147.html

相关文章:

  • Linux操作系统基础题库
  • Java抽象类:为何它是你代码架构的基石?
  • Flutter 中的 ToggleButtons 小部件:全面指南
  • 【MYSQL】一颗B+树可以保存多少条数据
  • ssm125四六级报名与成绩查询系统+jsp
  • 【Unity从零开始学习制作手机游戏】第01节:控制3D胶囊体运动
  • 内容安全(DPI和DFI解析)
  • 2024数维杯数学建模A题B题C题思路+模型+代码(开赛后第一时间更新)
  • SpringSecurity多表,多端账户登录
  • 绝地求生PUBG新老艾伦格有什么差别 老艾伦格什么时候回归
  • Windows下安装Node.js、npm和electronic,并运行一个Hello, World!脚本程序
  • 【精品案例】化工炼化企业信息化建设解决方案(74页PPT)
  • 【Unity Animation 2D】Unity Animation 2D骨骼绑定与动画制作
  • 工器具管理(基于若依)
  • UE4_照亮环境_光束light beam
  • springboot3项目练习详细步骤(第三部分:文章管理模块)
  • 【面试八股总结】C++11新特性:智能指针
  • 【教程向】从零开始创建浏览器插件(二)深入理解 Chrome 扩展的 manifest.json 配置文件
  • 美易官方:美国房地产贷款逾期率飙升,银行业危机仍可控?现货黄金暂守2360
  • SwiftUI中的@StateObject和@ObservedObject的区别
  • 类与对象(二)
  • LeetCode/NowCoder-链表经典算法OJ练习2
  • 英伟达解码性能NVDEC
  • 文心一言 VS 讯飞星火 VS chatgpt (255)-- 算法导论18.3 1题
  • C++ | Leetcode C++题解之第73题矩阵置零
  • 用 Supabase CLI 进行本地开发环境搭建
  • 三极管 导通条件
  • 一次pytorch分布式训练精度调试过程
  • STM32(GPIO)
  • python设计模式---观察者模式