PHP 5.4.36 Released

La classe SplEnum

(PECL spl_types >= 0.1.0)

Introduction

La classe SplEnum est utilisée pour émuler et créer des objets d'énumération nativement en PHP.

Synopsis de la classe

SplEnum extends SplType {
/* Constantes */
const NULL __default = null ;
/* Méthodes */
public array getConstList ([ bool $include_default = false ] )
/* Méthodes héritées */
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )
}

Constantes pré-définies

SplEnum::__default

Exemples

Exemple #1 Exemple d'utilisation de la classe SplEnum

<?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;
}
?>

L'exemple ci-dessus va afficher :

6
Value not a const in enum Month

Sommaire

add a note add a note

User Contributed Notes 2 notes

up
12
No Such Alias
4 years ago
Here's a clearer example usage in case anyone else finds the
current documentation confusing (as I did).

<?php
class Fruit extends SplEnum
{
 
// If no value is given during object construction this value is used
 
const __default = 1;
 
// Our enum values
 
const APPLE     = 1;
  const
ORANGE    = 2;
}

$myApple   = new Fruit();
$myOrange  = new Fruit(Fruit::ORANGE);
$fail      = 1;

function
eat(Fruit $aFruit)
{
  if (
Fruit::APPLE == $aFruit) {
    echo
"Eating an apple.\n";
  } elseif (
Fruit::ORANGE == $aFruit) {
    echo
"Eating an orange.\n";
  }
}

eat($myApple);  // Eating an apple.
eat($myOrange); // Eating an orange.

eat($fail); // PHP Catchable fatal error:  Argument 1 passed to eat() must be an instance of Fruit, integer given

?>
up
1
lsloan-php dot net at umich dot edu
9 days ago
SplEnum is a nice start for enumerated type support as an extension (making enum a part of the language would be much better), but it lacks a lot of features that enums have in other languages.  I needed functionality like the hasKey() method supported by Java.  I extended the class as SplEnumPlus and used that subclass in my code as follows:

<?php
class SplEnumPlus extends SplEnum {
    static function
hasKey($key) {
       
$foundKey = false;
       
        try {
           
$enumClassName = get_called_class();
            new
$enumClassName($key);
           
$foundKey = true;
        } finally {
            return
$foundKey;
        }
    }
}

class
Fruit extends SplEnumPlus {
  const
APPLE     = 1;
  const
ORANGE    = 2;
}

echo (
Fruit::hasKey(Fruit::APPLE) ? 'yes' : 'no') . PHP_EOL; // yes
echo (Fruit::hasKey('banana') ? 'yes' : 'no') . PHP_EOL; // no
?>

Other useful features, like reverse value-to-key lookups, could be done in this way.  It would be helpful if this and other useful functionality were made part of SplEnum.
To Top