Enumerations in PHP - GeeksforGeeks

24 Jan, 2019
Enumeration (or enum) is mainly used to assign names to integral constants, the names make a program easy to read and maintain. In PHP, enumeration datatypes can be implemented using and extending abstract classes.
Approach 1: Using simple abstract class to for data member encapsulation.
Example:
<?php// PHP program to show use of enumerations  // Encapsulating constantsabstract class gfg {    const dummy_string = "geeksforgeeks";    const dummy_int    = 1;    const dummy_array  = array('a' => 1, 'b' => 2);}  $a = gfg::dummy_string;$b = gfg::dummy_int;$c = gfg::dummy_array;  var_dump($a);var_dump($b);var_dump($c);  ?>
string(13) "geeksforgeeks"
int(1)
array(2) {
  ["a"]=>
  int(1)
  ["b"]=>
  int(2)
}

Approach 2: Extend an abstract class acting as an enumeration container for encapsulating constants.
Example:
<?php// PHP program to show use of enumerations  // Base enumeration classabstract class enum {      // Enumeration constructor    final public function __construct($value) {        $this->value = $value;    }      // String representation    final public function __toString() {        return $this->value;    }}  // Encapsulating enumerated constantsclass gfg extends enum {    const dummy_string = "geeksforgeeks";    const dummy_int    = 1;    const dummy_array  = array('a' => 1, 'b' => 2);}  $a = new gfg(gfg::dummy_string);$b = new gfg(gfg::dummy_int);$c = new gfg(gfg::dummy_array);  var_dump($a);var_dump($b);var_dump($c);  ?>
object(gfg)#1 (1) {
  ["value"]=>
  string(13) "geeksforgeeks"
}
object(gfg)#2 (1) {
  ["value"]=>
  int(1)
}
object(gfg)#3 (1) {
  ["value"]=>
  array(2) {
    ["a"]=>
    int(1)
    ["b"]=>
    int(2)
  }
}

Approach 3: Enumeration mentioned in the previous approach can be specialized by addition of validity checks and exception handling for more flexible usage of enumeration datatype.
Example:
<?php// PHP program to show use of enumerations  // Base enumeration classabstract class enum {      // Enumeration constructor    final public function __construct($value) {        try {            $c = new ReflectionClass($this);              // Content validation            if(!in_array($value, $c->getConstants())) {                try {                    throw new                     Exception("IllegalArgumentException");                }                catch (Exception $k) {                    echo $k->getMessage();                }            }            $this->value = $value;        }        catch (Exception $k){            echo $k->getMessage();        }    }      // String representation    final public function __toString() {        return $this->value;    }}  // Encapsulating enumerated constantsclass gfg extends enum {    const dummy_string = "geeksforgeeks";    const dummy_int    = 1;    const dummy_array  = array('a' => 1, 'b' => 2);}  $a = new gfg(gfg::dummy_string);$b = new gfg(gfg::dummy_int);$c = new gfg(gfg::dummy_array);$d = new gfg(3.14);  var_dump($a);var_dump($b);var_dump($c);var_dump($d);?>
IllegalArgumentExceptionobject(gfg)#1 (1) {
  ["value"]=>
  string(13) "geeksforgeeks"
}
object(gfg)#2 (1) {
  ["value"]=>
  int(1)
}
object(gfg)#3 (1) {
  ["value"]=>
  array(2) {
    ["a"]=>
    int(1)
    ["b"]=>
    int(2)
  }
}
object(gfg)#4 (1) {
  ["value"]=>
  float(3.14)
}

Note: PHP has SplEnum class which can be used for enumerations, though the implementation is not available in all stable versions of PHP.
PHP-basics
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
Comparing two dates in PHP
How to receive JSON POST with PHP ?
PHP | Converting string to Date and DateTime
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
Comparing two dates in PHP
How to fetch data from localserver database and display on HTML table using PHP ?