Page 1 of 1

Enum in PHP the enumerated datatype in PHP Rate Topic: -----

#1 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4192
  • View blog
  • Posts: 13,244
  • Joined: 08-June 10

Post icon  Posted 23 November 2010 - 08:49 AM


Enums are part of many major programming languages like C++, Java or Ada (even SQL has an ENUM() type)) and they are used to provide a predefined set of named values.
For a long time, PHP has been missing such an Enum implementation and people started to write their own Enum classes. With the development of the SPL, there has been a major step towards implementing programming features yet missing in PHP (e.g. Exceptions and Iterators), one of those being datatypes.

what you need
  • a working installation of PHP 5
  • the "SPL_Types" Extension (available via PECL)
  • OOP basics

recommended reading
Enumerated Datatype (or something similar, so you know what Enum is about)

installation of the SPL_Types Extension

Users of MacPorts can install SPL_Types by:
sudo port install php5-SPL_Types

all others have to install the PEAR/PECL subsystem and follow the instructions given in the manual1.

Be aware that the SPL_Types Extension is still labelled as "Experimental".

What is an Enum in PHP?

Enums in PHP are implemented through SPL’s abstract SplEnum class2. To define an Enum class you need to
- extend SplEnum
- define the Enum’s members as class constant3

Additionally, you can define a default value by setting the __default constant to any one of the members’ value.

Ex. 1:
class Suit extends SplEnum
          __default = 0
        , CLUBS     = 0
        , SPADES    = 1
        , HEARTS    = 2
        , DIAMONDS  = 3

Because Enums are defined on class level, you cannot define them at runtime.

To instantiate an Enum member do:
$spades = new Suit(Suit::SPADES);

The variable $spades now represents the SPADES member of the Suit Enum.

To have an Enum member as object of a class gives you the advantage of using Type-Hinting in your function/method definitions.
function setCardSuit(Suit $col)

The function will now accept only Suit objects, i.e. you restricted the function to accept only card suits (no further validation required)!

All this is good, but how do you test, whether a certain Enum member was specified?
This is done by comparing the Enum object’s value
var_dump($spades == Suit::CLUBS);
var_dump($spades == 1);

Bear in mind that you must not use the Identical operator (===) as an Enum object is not an integer.

To illustrate the use of Enums, a simple example for a person class.

Ex. 2
// defining a 2-member Enum w/o defaults
// i.e. you must pass a parameter to the constructor
class Gender extends SplEnum
          MALE   = 1
        , FEMALE = 2

// define a Person object with name, age and gender properties
class Person
        , $age
        , $gender

    public function __construct($name, $age, Gender $gender)
        $this->name   = $name;
        $this->age    = (int) $age;
        $this->gender = $gender;
    public function isMale()
        return ($this->gender == Gender::MALE); 
    public function isFemale()
        return ($this->gender == Gender::FEMALE);

$jd = new Person("John Doe", 20, new Gender(Gender::MALE));
echo $jd->isFemale(); // false
echo $jd->isMale();   // true

1 – you may guess that I run MacPorts’ PHP …

2 – class synopsis (Version 0.3 by Reflection, as the manual is inconsistent here)
abstract SplEnum extends SplType
	// Constants
	__default = null

	// Methods
	public array getConstList( void )

	// Methods inherited from SplType
	__construct( mixed $input )

3 – the Manual says the value must be an integer, although it also works with strings and possibly all datatypes accepted in constants.

This post has been edited by Dormilich: 15 March 2011 - 11:10 PM

Is This A Good Question/Topic? 5
  • +

Replies To: Enum in PHP

#2 aaron1178   User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,308
  • Joined: 22-October 08

Posted 23 November 2010 - 02:07 PM

Another great tutorial Dormilich.
Great, i've sometimes wanted to use Enumerators but could not because i did not want to write my own classes, this might help me down the track.
Thanks +1

Thanks Aaron1178
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1