0

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?

4
  • I don't think there is a solution. And array is only available since PHP 7 Commented Jan 5, 2018 at 13:54
  • 1
    Autocompletion of what? Are you talking about your IDE? Commented Jan 5, 2018 at 14:01
  • @Oldskool yes. the idea does not autocomplete array keys Commented Jan 5, 2018 at 14:25
  • and refactoring (e.g changing the keyname will not work. That's why I would prefer class properties Commented Jan 5, 2018 at 14:26

2 Answers 2

3

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

class Activities
{
    public static $running = ['title' => 'running', 'id' => 1];
}
Sign up to request clarification or add additional context in comments.

1 Comment

I currently have only PHP 5.3 at hand and const didn't work. Perhaps in newer versions of PHP it will work indeed.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.