-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Open
Description
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?
cedx, hemberger, rotdrop, liquid207, newtype0092 and 13 moreSuitespaceDev, samuelgfeller, hemberger, TheApproach, newtype0092 and 1 more