Chris
Chris

Reputation: 14208

php multidimensional enum (const)

I have an activities enum:

class Activities
{
    const running = 'running';
    ...
}

Now I would like to add an id to the constant:

class Activities
{
    const running = ['title' => 'running', 'id' => 1]
    ...
}

Unfortunately I do not get autocompletion when using an array. I would therefore prefer to use a class instead of the array.

However this is not allowed for a const. (expression is not allowed as class constant value)

Is there a solution that enables autocompletion for this case?

Upvotes: 0

Views: 430

Answers (2)

Yuriy Semenikhin
Yuriy Semenikhin

Reputation: 90

This will be either using an array, then autocompletion becomes a bit cumbersome but still possible (if I understand your intentions correctly) you just going to have to run it against $array['activity']

Or you a going to have to create a class

class Activity {
    const activity='';
    const id='';
    cont etc='...';
}

and then you will need to instantiate a new Activity for every one that you want to play with.

But not sure on how it will be any less cumbersome then an array.

Sorry if the answer is slightly convoluted, it's been a while since I had to think about php in any sort of distinguishably human language.

Upvotes: 0

Marcovecchio
Marcovecchio

Reputation: 1332

You can remove const and define your enums as public static:

class Activities
{
    public static $running = ['title' => 'running', 'id' => 1];
}

Upvotes: 3

Related Questions