Enum
class for using enums in PHP
- Yii
framework.
Two type of enums can be instanced: regular Enum
s and DBEnum
s. 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
Enum
s so you'll have to changeEnum
calls if you go with this version. ThegetEnum()->
part of the interface has been stripped out, now the interface looks likeMyEnum::getValidValues()
. - Fixed a bug with radio button list
- The newer version file is enums2.zip
Notes ¶
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.
Requirements ¶
php >= 5.3.0 (PHP Reflection API required)
Usage ¶
- Declare an
Enum
: subclassEnum
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
: subclassDBEnum
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 Enum
s and DBEnum
s
- I18N:
Enum
supports i18n through the Yii API.Enum
translations are taken from a category calledenums
so to define your own translations just create a fileenums.php
file undermessages/<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;
My implementations as a LookUp Class
just for the same functionality i have created a lookUp where are declared const values
and methods for populating various kinds of Lists & selects from the DB.
php >= 5.3.0
This extension uses the get_called_class function which is only available in php 5.3.0 (or more).
php >= 5.3.0
@Raoul Your absolutely right. I've updated the documentation.
Enum translations are override, how to handle them for multiple Enums Classes?
As ItemStatusEnum::CONFIRMED and DepartmentRefEnum::SEATTLE have same reference number reffered as 1 therefore therefore of ItemStatusEnum::CONFIRMED is also Seattle.
Is there idea, how I can handle this in Enums.php?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.