SPL Types
PHP Manual

Die Klasse SplEnum

(PECL spl_types >= 0.1.0)

Einführung

SplEnum bietet die Möglichkeit Aufzählungen (Enums) und Enum-Objekte nativ in PHP zu erstellen.

Klassenbeschreibung

SplEnum extends SplType {
/* Constants */
const NULL __default = null ;
/* Methoden */
public array getConstList ([ bool $include_default = false ] )
/* Geerbte Methoden */
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )
}

Vordefinierte Konstanten

SplEnum::__default

Beispiele

Beispiel #1 SplEnum Verwendungsbeispiel

<?php
class Month extends SplEnum {
    const 
__default = self::January;
    
    const 
January = 1;
    const 
February = 2;
    const 
March = 3;
    const 
April = 4;
    const 
May = 5;
    const 
June = 6;
    const 
July = 7;
    const 
August = 8;
    const 
September = 9;
    const 
October = 10;
    const 
November = 11;
    const 
December = 12;
}

echo new 
Month(Month::June) . PHP_EOL;

try {
    new 
Month(13);
} catch (
UnexpectedValueException $uve) {
    echo 
$uve->getMessage() . PHP_EOL;
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

6
Value not a const in enum Month

Inhaltsverzeichnis


SPL Types
PHP Manual