[SOLVED] What is preferred enumeration technique in YII?

Hello friends,

Unfortunately I didn’t find any recomendations on how to implement enumerable properties.

A single approch I’ve seen in YII apps is to declare certain class constants and use them

as property values. However, this is not that trivial. Imagine we have TTestManager app

component with it’s enumProperty. Also, there are 2 constants declared in a TTestManager


const EP_VALUE_1 = 1; const EP_VALUE_2 = 2;

This way, our YII configuration might look like one of the following:

'testManager' => array('class' => 'TTestManager', 'enumProperty' => 1)

'testManager' => array('class' => 'TTestManager', 'enumProperty' => TTestManager::EP_VALUE_1)

First case is absolutely unintuitive. Who knows what this ‘1’ actually means?

Second is perfect for low-performance applications. It will load TTestManager class every time

configuration is read.

So, I came to a conclusion that best practice for YII enumerable properties is the following:

const EP_VALUE_1 = 'EP_VALUE_1'; const EP_VALUE_2 = 'EP_VALUE_2';

'testManager' => array('class' => 'TTestManager', 'enumProperty' => 'EP_VALUE_1')

Though this solution is more viable than above mentioned, I still got the feeling of

inventing standards. It’d be great to have some core guide/support for this feature.

The second aproach is supported by the framework. And I don’t think this will imply a notable performance decrease… But if it really is too slow, you could always use some sort of cache to prevent the configuration from being read again and again with each request.

See http://www.yiiframework.com/doc/api/CEnumerable for more details.

Well, thanks! Didn’t find CEnumerable.php and was thinking it’s not implemented.

Problem solved!

If you are gona declare the strings by hand, why declare the constants? Put them in comments and they wont even take parsing time.

The TTestManager::EP_VALUE_1 version would make sense for IDEs (auto-completion), but the secund one (because it doesnt need or declare the constants), even tho its faster, beats the purpose of having constants doesnt it?

Well, in most cases you really do want to have and use constants. Not necessarily the language feature ‘constant’, but some individual value (however this is implemented) that you can reference from multiple locations of your source code. Otherwise (e.g. if using string values that you copy & paste or simply type in every time) you might end up searching all occurences of this particular value if you ever need to change it for some reason. So it is always better to define it at one location and to make sure it can’t change during script execution.

The enum base class provides another important feature. Using enums, you can make sure a certain variable or member of a class can only hold a given number of values! Only those that you have defined are acceptable, nothing else.

I think this confidence easily beats the performance increase you might experience if using plain strings or integers compared to enum values and constants.

Will be using CEnumerable + string initialization in config. Thanks 2 all.