Skip to content

Enum case as Array key? #9208

@geraldofebrian

Description

@geraldofebrian

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions