473,791 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9457
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_FIS H', 3);
echo ALL_CAT;

--
E. Dronkert
Nov 7 '05 #3
"Ewoud Dronkert" <fi*******@last name.net.invali d> wrote in message
news:iu******** *************** *********@4ax.c om...
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_FIS H', 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*********@gm ail.com> wrote in message
news:Ie******** ************@co mcast.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
4417
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 I've seen I can define both keys for description and code, but my XMLs validate even if I choose a wrong pair: Here are my both enumerations (abbreviated) <xs:simpleType name="COUNTRYCODE"> <xs:restriction base="xs:int"> <xs:enumeration...
0
1179
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 parameters, as well as output Events. Public Enum enmListOfValues Value1 Value2 Value3 Value4
21
1967
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 defined in foo.cpp needs to return typeNone. Is using static_cast< bar >( 0 )
3
1708
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 DLL I'd like to access -- is this possible? Thanks, Joe
1
2149
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 PropertyGrids displaying these enumerations (one of which has a few hundred items in). I believe this is because the dynamically generated enumerations are not being destroyed. I have not been able to find a mechanism for deleting/destroying dynamically...
1
5613
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 base="xs:string"> <xs:enumeration value="Apple" /> <xs:enumeration value="Orange" /> </xs:restriction>
4
3539
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. Should a single class be created that contains all of a solution's enumerations? Or should enumerations be defined directly in the files that contain the related classes? Are there any other design patterns I am overlooking?
27
2678
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 provide a "one obvious way" to do enumerations in Python. > >>> from enum import Enum > >>> day = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')
77
3957
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 reStructuredText source as it is today. Please discuss it here so I can see what issues people may have.
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.