PHP 使用反射获取常量名、值及注释

本文介绍了如何在PHP中利用ReflectionClassConstant类实现枚举类的常量名、值以及注释的获取,包括getConst(), getConstantsKeys(), getConstantsValues(), getDescription()等方法的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PHP 使用反射获取常量名、值及注释

基类代码如下:

<?php

namespace app\common\enums;

use ReflectionClassConstant;

class BaseEnum
{
    /**
     * 获取所有常量
     * @return array
     */
    public static function getConst(): array
    {
        $objClass = new \ReflectionClass(get_called_class());
        return $objClass->getConstants();
    }

    /**
     * 获取所有常量名
     * @return array
     */
    public static function getConstantsKeys(): array
    {
        return array_keys(self::getConst());
    }

    /**
     * 获取所有常量值
     * @return array
     */
    public static function getConstantsValues(): array
    {
        return array_values(self::getConst());
    }

    /**
     * 获取常量注释
     * @param string $key 常量名
     * @return string
     */
    public static function getDescription(string $key): string
    {
        return preg_replace('#[\*\s]*(^/|/$)[\*\s]*#', '', (new ReflectionClassConstant(static::class, $key))->getDocComment());
    }

    /**
     * 获取常量名和注释列表
     * @return array
     */
    public static function getKeyDescription(): array
    {
        $keys   = self::getConstantsKeys();
        $result = [];

        foreach ($keys as $key => $key_name) {
            $result[$key_name] = self::getDescription($key_name);
        }

        return $result;
    }

    /**
     * 获取常量值和注释列表
     * @return array
     */
    public static function getValueDescription(): array
    {
        $const  = self::getConst();
        $result = [];

        foreach ($const as $key => $value) {
            $result[$value] = self::getDescription($key);
        }

        return $result;
    }
}

子类代码如下:

<?php

namespace app\common\enums;

class PacketStatus extends BaseEnum
{
    /** 未领取 */
    const UNCLAIMED = 0;
    /** 未发放 */
    const READY = 1;
    /** 处理中 */
    const PROCESSING = 2;
    /** 发放成功 */
    const SUCCESS = 3;
    /** 发放失败 */
    const FAIL = 4;
}

如何使用

获取所有常量
// 获取所有常量
PacketStatus::getConst()

// 返回结果:
array(5) {
  ["UNCLAIMED"] => int(0)
  ["READY"] => int(1)
  ["PROCESSING"] => int(2)
  ["SUCCESS"] => int(3)
  ["FAIL"] => int(4)
}
获取所有常量名
// 获取所有常量名
PacketStatus::getConstantsKeys()

// 返回结果:
array(5) {
  [0] => string(9) "UNCLAIMED"
  [1] => string(5) "READY"
  [2] => string(10) "PROCESSING"
  [3] => string(7) "SUCCESS"
  [4] => string(4) "FAIL"
}
获取所有常量值
// 获取所有常量值
PacketStatus::getConstantsValues()

// 返回结果:
array(5) {
  [0] => int(0)
  [1] => int(1)
  [2] => int(2)
  [3] => int(3)
  [4] => int(4)
}
获取常量名和注释列表
// 获取常量值和注释列表
PacketStatus::getKeyDescription()

// 返回结果:
array(5) {
  ["UNCLAIMED"] => string(9) "未领取"
  ["READY"] => string(9) "未发放"
  ["PROCESSING"] => string(9) "处理中"
  ["SUCCESS"] => string(12) "发放成功"
  ["FAIL"] => string(12) "发放失败"
}
获取常量值和注释列表
// 获取常量值和注释列表
PacketStatus::getValueDescription()

// 返回结果:
array(5) {
  [0] => string(9) "未领取"
  [1] => string(9) "未发放"
  [2] => string(9) "处理中"
  [3] => string(12) "发放成功"
  [4] => string(12) "发放失败"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值