devman42 Posted January 10, 2009 Share Posted January 10, 2009 Is it possible to use enums in Php like in Java? I know there isn't an enum type in php, but is there a best practice or work around for achieving similar functionality. Thanks for any advise/resource examples. I would like to create an enum class for something like "Address Types" where it is really just a collection of constant ids + name combinations. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/140342-enums-in-php/ Share on other sites More sharing options...
Mchl Posted January 10, 2009 Share Posted January 10, 2009 There is no mechanism that I'm aware of, which would allow this. PHP is loosely typed and that's it. You only have notices that will warn you of undefined indexes in arrays for example. Quote Link to comment https://forums.phpfreaks.com/topic/140342-enums-in-php/#findComment-734369 Share on other sites More sharing options...
DarkWater Posted January 11, 2009 Share Posted January 11, 2009 They're working on implementing it in the SPL. http://php.net/manual/en/splenum.construct.php Quote Link to comment https://forums.phpfreaks.com/topic/140342-enums-in-php/#findComment-734457 Share on other sites More sharing options...
Daniel0 Posted January 11, 2009 Share Posted January 11, 2009 Maybe I'm stupid, but I've never seen the need for an enum. Quote Link to comment https://forums.phpfreaks.com/topic/140342-enums-in-php/#findComment-734618 Share on other sites More sharing options...
corbin Posted January 11, 2009 Share Posted January 11, 2009 Maybe I'm stupid, but I've never seen the need for an enum. That's what I was just thinking. They seem like a waste of overhead. Quote Link to comment https://forums.phpfreaks.com/topic/140342-enums-in-php/#findComment-734713 Share on other sites More sharing options...
dbo Posted January 11, 2009 Share Posted January 11, 2009 I'd be inclined to agree with Daniel0 and corbin. That being said I've seen people emulate enums through the use of associative arrays. Have you given that any thought? Quote Link to comment https://forums.phpfreaks.com/topic/140342-enums-in-php/#findComment-734834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.