PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 7th, 2002, 09:12 PM
scorpion scorpion is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 7 scorpion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question php and enumerations

im puzzled. i use vbscript and c++ etc and they have enumerations. i can't find how to create an enum in php, does anyone have any ideas? i looked in the manual..no hope. i want something like this

var $myEnum {0 = 'red', 1 = 'blue', 2 = 'yellow'};
$d = new myEnum('blue');

i suppose i could use a class but its a waste if php does actually support enums.

Reply With Quote
  #2  
Old April 7th, 2002, 11:16 PM
billsanders billsanders is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Oklahoma, USA
Posts: 13 billsanders User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to billsanders
Can you just use an array?

Consider:
$myEnum = array ("red","blue","yellow");
$d = $myEnum[1];

Reply With Quote
  #3  
Old April 8th, 2002, 03:00 AM
Spyder Spyder is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Australia
Posts: 5 Spyder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to Spyder Send a message via AIM to Spyder Send a message via Yahoo to Spyder
or maybe an associative array?

PHP Code:
 $myEnum = array ("red" => 1,"blue" => 2,"yellow" => 3); 
$d $myEnum["red"]; 



I didn't see any direct references to enums in the PHP manual but I've never had much use for them...

Reply With Quote
  #4  
Old April 8th, 2002, 06:03 AM
scorpion scorpion is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 7 scorpion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i guess i could yeah...but im assuming that no one knows if php supports enums? if not its ok...an associative array will do. thanks guys

Reply With Quote
  #5  
Old April 8th, 2002, 12:48 PM
jasonlotito jasonlotito is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Montreal, CA
Posts: 10 jasonlotito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to jasonlotito
ENUM's don't exists in PHP. No real need for them, as PHP's arrays can pretty much handle the job.

Reply With Quote
  #6  
Old December 24th, 2008, 10:27 AM
emptycrawford emptycrawford is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 1 emptycrawford User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 27 sec
Reputation Power: 0
a decent workaround

I got to this thread trying to find out the same thing. I didn't really get my question answered, but I did think of a nasty workaround.
PHP Code:
 $username 0;
$password 1;
// ... etc ...

$user = array('johndoe','h1ghw1nd5');

// after that, you can use it as an 'enum', somewhat


if(valid($user[$username], $user[$password]))
// login
else
// try again 

Reply With Quote
  #7  
Old August 31st, 2009, 12:18 PM
shellwes shellwes is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 1 shellwes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 48 sec
Reputation Power: 0
Howto Simulate Enumeration in PHP

Hey all,

This is how I implement enumeration in php. It is a little tighter then using an array so you have less bugs later.

I can't post the link but you can look it up detailed explanation on my blog at wordpress. wshell.wordpress.com

Here is the code...

PHP Code:
Interface IEnum
{
    function 
__constructor($_value "")

    public function 
Value( )
    public function 
Set($_value)
}

Class 
Shape implements IEnum
{
    private 
$m_value;

    function 
__constructor($_value "")
    {
        if (
$_value != "")
        {
            
$this->Set($_value);
        }
    }    

    public function 
Value()
    {
        return 
$this->m_value;
    }

    public function 
Set($_value)
    {
        switch(
$_value)
        {
        case 
"Square":
        case 
"Circle":
        case 
"Triangle":
        case 
"Rectangle":
        
$this->m_value $_value;
        break;
        default:
        throw new 
Exception("Enumeration Error, Set
                    value (" 
$_value ") is not an option for
                    this Enumeration"
);
    }
    }



Working Example Using Constructor:
$shape = new Shape("Circle");
echo $shape->Value();

Output: Circle

Working Example Using Set():
$shape = new Shape();
$shape->Set("Square");
echo $shape->Value();

Output: Square

Exception Example Using Set():
$shape = new Shape();
$shape->Set("Trapazoid");
echo $shape->Value();

Output: Enumeration Exception.

Enjoy!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > php and enumerations


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap