Page 1 of 1

Enumerated Types

Posted: Wed Nov 13, 2002 8:35 pm
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!

Posted: Wed Nov 13, 2002 10:23 pm
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?

Posted: Wed Nov 13, 2002 11:29 pm
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

Posted: Wed Nov 13, 2002 11:49 pm
by protokol

Posted: Thu Nov 14, 2002 4:03 am
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