1

I have declared some values in an enum class as below,

enum EmployeeGroup: int
    {
        case ADMINISTRATOR = 1;
        case GROUP_DEFAULT = 2;
        case DIRECTOR = 3;
        case DUH = 4;
        case TEAM_LEAD = 5;
        case SUPER_USER = 6;
        case HR = 7;
        case PROJECT_MANAGER = 8;
        case ENGINEER = 9;
        case SENIOR_ENGINEER = 10;
        case FUNC_LEADS = 11;
        case SUPPORT_MEMBERS = 12;
    }

I used them in another class s below. This static variable needs an array of values as its input.

class TeamMenu extends Menu
{
    /**
     * Permitted user groups to view menu
     * 
     * @var array
     */
    static $permitted = [
        EmployeeGroup::DUH->value,
        EmployeeGroup::HR->value,
        EmployeeGroup::PROJECT_MANAGER->value,
        EmployeeGroup::ENGINEER->value,
        EmployeeGroup::SENIOR_ENGINEER->value,
    ];
}

So, what I need is to get the integer values from enum class and include them in $permitted variable. But, when I run this code, I get an error saying,

Constant expression contains invalid operations

What is the reason for this error? (My php version is 8.1)

  • Does this answer your question? [PHP Error : Fatal error: Constant expression contains invalid operations](https://stackoverflow.com/questions/40171546/php-error-fatal-error-constant-expression-contains-invalid-operations) – CBroe Jul 21 '22 at 14:03

0 Answers0