php / php/php-src

Enum case as Array key?

未关闭
#9,208 56 条评论 24 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Feature Status: Requires RFC Status: Verified
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

Before enum exist, i used constant to define the array key. Now i want to use enum. But i got error when i declare the enum as array key. so i do the workaround like this:

if (!function_exists('_api')) {
    /**
     * use enum as Array Key.
     *
     * @param array $values
     * @param \App\Enums\Api\ResponseKey $key
     * @param mixed $data
     * @return array
     */
    function _api(?array &$values, ResponseKey $key, $data)
    {
        if (! is_array($values)) $values = [];

        $values[$key->value] = $data;

        return $values;
    }
}

Then call it like _api($data, ResponseKey::message, "success");

Can this workaround can be implemented as feature for future PHP?

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

未指定任何文件或测试。首先阅读 issue 讨论并检查 PHP enum 和 array-key 的行为,然后找到相关的语言实现和回归测试。完成这项工作需要达成一致的语言设计,并实现对预期 enum 键行为的覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
php
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。