...

View Full Version : How to Emulate Enum in PHP like C?



dealmaker
01-05-2006, 02:15 AM
Hi,
In C language, there is enum, and I can do the following:



enum {
DOG,
CAT,
BIRD,
MAX_NUM_OF_ANIMALS
};


There is no enum in php, how can I emulate enum in php? I want to do better than just use define() because I need to put use numbers like this "define(DOG, 0); define(CAT, 1);" and there is no way to access MAX_NUM_OF_ANIMALS without doing another define.

Many thanks.

nikkiH
01-05-2006, 02:24 AM
Use an associative array.
http://www.hypermedic.com/php/arrays3.htm

C doesn't let you refer to array index as a string. PHP (among others) does.

If that doesn't suit you, you can make an enum class yourself... ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum