PHP Enumerations?
am 06.11.2005 23:40:05 von Seamus M
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
Re: PHP Enumerations?
am 07.11.2005 02:16:16 von catch
Seamus M said the following on 06/11/2005 22:40:
> 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
>
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
Re: PHP Enumerations?
am 07.11.2005 10:40:56 von Ewoud Dronkert
Oli Filth wrote:
> Seamus M said the following on 06/11/2005 22:40:
>> I can't find any info on enumerations in the PHP manual
>
> 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"
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_FISH', 3);
echo ALL_CAT;
--
E. Dronkert
Re: PHP Enumerations?
am 08.11.2005 09:20:29 von Dana Cartwright
"Ewoud Dronkert" wrote in message
news:iu7um1151i46lhunv0070528r8bdgat13a@4ax.com...
> Oli Filth wrote:
>> Seamus M said the following on 06/11/2005 22:40:
>>> I can't find any info on enumerations in the PHP manual
> Or constants, but they are not restricted to one type or container:
>
> define('ALL_CAT', 1);
> define('ALL_DOG', 2);
> define('ALL_FISH', 3);
> echo ALL_CAT;
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.
Re: PHP Enumerations?
am 08.11.2005 23:36:46 von cl1mh4224rd
============================================================ ============
Dana Cartwright wrote:
> 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.
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
Re: PHP Enumerations?
am 09.11.2005 05:34:29 von Dana Cartwright
"Ryan Lange" wrote in message
news:IeCdnRQmNqjgtezeRVn-pA@comcast.com...
> ============================================================ ============
> Dana Cartwright wrote:
>> 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++ );
>>
> Keeping with cat=1, dog=2, and fish=3, shouldn't that be either:
>
> $n = 1;
>
> ...or...
>
> $n = 0;
> define('ALL_CAT', ++$n);
>
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.