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

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) "发放失败"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,枚举类型是一种特殊的类,它定义了一组有限的常量值。枚举常量名称通常是大写字母,但是它们也可以有一个与之对应的字符串值。如果一个枚举常量有一个字符串值,那么可以通过调用枚举常量的name()方法获取枚举常量名称,或者调用枚举常量的toString()方法获取枚举常量的字符串值。 下面是一个示例代码,演示了如何获取枚举常量的字符串值: ``` enum Color { RED("红色"), GREEN("绿色"), BLUE("蓝色"); private String value; private Color(String value) { this.value = value; } public String getValue() { return value; } } public class Main { public static void main(String[] args) { Color color = Color.GREEN; String name = color.name(); String value = color.getValue(); String str = color.toString(); System.out.println(name); // 输出:GREEN System.out.println(value); // 输出:绿色 System.out.println(str); // 输出:绿色 } } ``` 在这个例子中,我们首先定义了一个枚举类型Color,包含三个常量:RED、GREEN和BLUE。每个枚举常量都有一个与之对应的字符串值,存储在枚举常量的value属性中。Color类还定义了一个getValue()方法,用于获取枚举常量的字符串值。 在main()方法中,我们定义了一个Color类型的变量color,并将其赋值为枚举常量GREEN。然后,我们分别调用color的name()方法、getValue()方法和toString()方法,获取枚举常量名称、字符串值和toString()方法返回的字符串值。最后,我们输出了这三个值,分别为GREEN、绿色和绿色。 需要注意的是,如果一个枚举常量没有对应的字符串值,那么调用其getValue()方法会返回null,调用其toString()方法会返回枚举常量名称

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值