PHP Enumerations?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Seamus M

    PHP Enumerations?

    I can't find any info on enumerations in the PHP manual, so I assume there
    is no built in way to create them. Can anyone tell me the best way to build
    a simple enumeration, such as:

    Enum AllItems as Integer

    'First Item' = 1;
    'Second Item' = 2;
    :
    End Enum



  • Oli Filth

    #2
    Re: PHP Enumerations?

    Seamus M said the following on 06/11/2005 22:40:[color=blue]
    > I can't find any info on enumerations in the PHP manual, so I assume there
    > is no built in way to create them. Can anyone tell me the best way to build
    > a simple enumeration, such as:
    >
    > Enum AllItems as Integer
    >
    > 'First Item' = 1;
    > 'Second Item' = 2;
    > :
    > End Enum
    >[/color]

    PHP does not have enumerations per se.

    I guess the closest thing you could do (assuming PHP 5) is an abstract
    class with constants, e.g.:

    abstract class AllItems
    {
    const Dog = 1;
    const Cat = 2;
    const Ocelot = 3;
    }

    echo AllItems::Dog; // displays "1"


    --
    Oli

    Comment

    • Ewoud Dronkert

      #3
      Re: PHP Enumerations?

      Oli Filth wrote:[color=blue]
      > Seamus M said the following on 06/11/2005 22:40:[color=green]
      >> I can't find any info on enumerations in the PHP manual[/color]
      >
      > I guess the closest thing you could do (assuming PHP 5) is an abstract
      > class with constants, e.g.:
      >
      > abstract class AllItems
      > {
      > const Dog = 1;
      > const Cat = 2;
      > const Ocelot = 3;
      > }
      >
      > echo AllItems::Dog; // displays "1"[/color]

      Or an associative array perhaps?

      $allitems = array('cat' => 1, 'dog' => 2, 'fish' => 3);
      echo $allitems['cat'];

      Or constants, but they are not restricted to one type or container:

      define('ALL_CAT ', 1);
      define('ALL_DOG ', 2);
      define('ALL_FIS H', 3);
      echo ALL_CAT;

      --
      E. Dronkert

      Comment

      • Dana Cartwright

        #4
        Re: PHP Enumerations?

        "Ewoud Dronkert" <firstname@last name.net.invali d> wrote in message
        news:iu7um1151i 46lhunv0070528r 8bdgat13a@4ax.c om...[color=blue]
        > Oli Filth wrote:[color=green]
        >> Seamus M said the following on 06/11/2005 22:40:[color=darkred]
        >>> I can't find any info on enumerations in the PHP manual[/color][/color][/color]
        [color=blue]
        > Or constants, but they are not restricted to one type or container:
        >
        > define('ALL_CAT ', 1);
        > define('ALL_DOG ', 2);
        > define('ALL_FIS H', 3);
        > echo ALL_CAT;[/color]

        You can get closer to the 'auto-increment' characteristic of true ENUM's by:

        $n = 0;
        define( 'ALL_CAT', $n++ );
        define( 'ALL_DOG', $n++ );
        define( 'ALL_FISH', $n++ );

        I suspect most C programmers, seeing the above, would say "Aha, it's an
        enumeration", which is really what one is after, I think.


        Comment

        • Ryan Lange

          #5
          Re: PHP Enumerations?

          =============== =============== =============== =============== ============
          Dana Cartwright wrote:[color=blue]
          > You can get closer to the 'auto-increment' characteristic of true ENUM's by:
          >
          > $n = 0;
          > define( 'ALL_CAT', $n++ );
          > define( 'ALL_DOG', $n++ );
          > define( 'ALL_FISH', $n++ );
          >
          > I suspect most C programmers, seeing the above, would say "Aha, it's an
          > enumeration", which is really what one is after, I think.[/color]

          Keeping with cat=1, dog=2, and fish=3, shouldn't that be either:

          $n = 1;

          ....or...

          $n = 0;
          define('ALL_CAT ', ++$n);

          ....etc?

          - Ryan

          Comment

          • Dana Cartwright

            #6
            Re: PHP Enumerations?

            "Ryan Lange" <cl1mh4224rd@gm ail.com> wrote in message
            news:IeCdnRQmNq jgtezeRVn-pA@comcast.com. ..[color=blue]
            > =============== =============== =============== =============== ============
            > Dana Cartwright wrote:[color=green]
            >> You can get closer to the 'auto-increment' characteristic of true ENUM's
            >> by:
            >>
            >> $n = 0;
            >> define( 'ALL_CAT', $n++ );
            >> define( 'ALL_DOG', $n++ );
            >> define( 'ALL_FISH', $n++ );
            >>[/color][/color]
            [color=blue]
            > Keeping with cat=1, dog=2, and fish=3, shouldn't that be either:
            >
            > $n = 1;
            >
            > ...or...
            >
            > $n = 0;
            > define('ALL_CAT ', ++$n);
            >[/color]

            C enumerations start at zero, which is why I wrote it that way. The OP
            talked about enumerations, and I was staying with that spirit.


            Comment

            Working...