Enumerated Types

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Enumerated Types

Post by protokol »

Well, I love PHP, but the one thing which I wish it had was enumerated data types. What are these you may ask? Well, they are defined in C and C++ like so:

enum Soda { Coke, Sprite, Pepsi };

Now basically what this means is that Coke == 0, Sprite == 1, and Pepsi == 2.

Code: Select all

// Proposed PHP usage
my_soda = enum { Coke, Sprite, Pepsi };

switch (my_soda) {
   case Coke:
      echo "Coke is great";
      break;
   case Sprite:
      echo "Sprite fizzles";
      break;
   case Pepsi:
      echo "Pepsi is fantastic";
      break;
   default:
      break;
}
Anyway, just thought these would be cool to have. If they exist, then please let me know. Take care all!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

function enum(){
$num = func_num_args();

    $arg_list = func_get_args();
    for ($i = 0; $i < $num; $i++) &#123;
    $&#123;$arg_list&#1111;$i]&#125; = $i;
    &#125;

&#125;
enum("coke", "pepsi", "sprite");
should make $coke 0, $pepsi 1, and $sprite 2

is this what you wanted?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Not quite .... i know it doesn't make a lot of sense to the PHP people ... maybe i can find a link where its use is explained in C++ code
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you may change hob_goblin's function to use define() but if you want the 'type-checking' of enums I doubt you can have it in php 4.2
Post Reply