downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

SplEnum::getConstList> <SplFloat
[edit] Last updated: Sat, 25 May 2013

view this page in

The SplEnum class

(Não há informação de versão disponível, talvez apenas no SVN)

Introdução

SplEnum gives the ability to emulate and create enumeration objects natively in PHP.

Sinopse da classe

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

Constantes pré-definidas

SplEnum::__default

Exemplos

Exemplo #1 SplEnum usage example

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

O exemplo acima irá imprimir:

6
Value not a const in enum Month

Índice



add a note add a note User Contributed Notes SplEnum - [1 notes]
up
-1
No Such Alias
2 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

?>

 
show source | credits | sitemap | contact | advertising | mirror sites