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

search for in the

SplEnum::__construct> <SplFloat::__construct
[edit] Last updated: Fri, 18 Sep 2009

view this page in

The SplEnum class

Увод

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

Синтаксис за класове

SplEnum
abstract SplEnum {
/* Methods */
__construct ( void )
}

Съдържание



add a note add a note User Contributed Notes SplEnum
No Such Alias 30-Jul-2010 03:11
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 | stats | sitemap | contact | advertising | mirror sites