enum Enum values in PHP - Yii

  1. Latest changes
  2. Notes
  3. Requirements
  4. Usage
  5. Support

Enum class for using enums in PHP - Yii framework.

Two type of enums can be instanced: regular Enums and DBEnums. The difference is that DBEnum enum values must exist in a database table (the ideal scenario for a DBEnum is an enumerated value in DB). A check against the database is performed before using DBEnum values. Take a look at the examples for further information.

Latest changes

  • Interface changed! Now it's simpler to call enum methods. WATCH OUT: this version is not compatible with previous Enums so you'll have to change Enum calls if you go with this version. The getEnum()-> part of the interface has been stripped out, now the interface looks like MyEnum::getValidValues().
  • Fixed a bug with radio button list
  • The newer version file is enums2.zip


If performance is a primary concern in you app, you should use the original version. Current version uses call_user_func_array which is slower than a direct call (not checked personally but according to some benchmarks it may be 12 times slower). I considered an acceptable trade off for the sake of a simpler interface.


php >= 5.3.0 (PHP Reflection API required)


  • Declare an Enum: subclass Enum class and define the enum values as constants:
class MyEnum extends Enum
  const MY_ENUM_VALUE1 = "MY_VALUE_1";
  const MY_ENUM_VALUE2 = "MY_VALUE_2";
  • Declare a DBEnum: subclass DBEnum class and define the enum values as constants. Also define the two abstract methods used to access the DB:
class MyEnum extends DBEnum
  const MY_ENUM_VALUE1 = "MY_VALUE_1";
  const MY_ENUM_VALUE2 = "MY_VALUE_2";

  protected function getDBField()
      return 'my_enum_id_field';

  protected function getDBTable()
      return 'myenum_table';

  // Optionally define a condition if only some values of 
  //the table are to be taken into consideration
  protected function getDBCondition()
      return "other_field=value";


The rest of the methods apply to both Enums and DBEnums

  • I18N: Enum supports i18n through the Yii API. Enum translations are taken from a category called enums so to define your own translations just create a file enums.php file under messages/<locale_id> and write your translations like this:
return array(
    MyEnum::MY_ENUM_VALUE1 => 'My value 1',
    MyEnum::MY_ENUM_VALUE2 => 'My value 2',
  • Check in model rules that a field has a valid enum value:
public function rules()
  return array( 
    array( 'enum_field', 'in', 'range' => MyEnum::getValidValues() ),
  • Fill a radio button list:
echo $form->radioButtonList( $model, 'enum_field', MyEnum::getDataForRadioButtonList() );
  • Fill a drop down list:
$form->dropDownList( $model, 'enum_field', MyEnum::getDataForDropDown() );
  • To use an individual enum value, use it as a constant
echo MyEnum::MY_ENUM_VALUE_1;


Support post

5 0
1 567 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Developed by: twocandles
Created on: Feb 6, 2011
Last updated: 7 years ago


show all