473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Nov 6 '05 #1
5 9440
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
Nov 7 '05 #2
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
Nov 7 '05 #3
"Ewoud Dronkert" <fi*******@lastname.net.invalid> wrote in message
news:iu********************************@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.
Nov 8 '05 #4
================================================== ======================
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
Nov 8 '05 #5
"Ryan Lange" <cl*********@gmail.com> wrote in message
news:Ie********************@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.
Nov 9 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
21
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method...
3
by: JoeH | last post by:
Hi, I'm using a COM DLL (created in VB) in my javascript code and can successfully call its methods and get/set its properties. There are also some Public enumerations defined in the ActiveX...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.