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

search for in the

SplBool> <SplEnum
Last updated: Fri, 20 May 2011

view this page in

SplEnum::__construct

(PECL spl_types >= 0.1.0)

SplEnum::__constructConstructs an enumeration object type

Descripción

SplEnum::__construct() ( void )

This constructor is used to set the value and the strictness of the enumeration object.

Parámetros

input

The input parameter accepts an integer and will throw an UnexpectedValueException if anything else is passed.

strict

A boolean variable to indicate whether the object's strictness should be set or not.

Valores devueltos

No devuelve ningún valor.

Ejemplos

Example #1 SplEnum::__construct() example

<?php

class EnumOne extends SplEnum
{
    const 
__default 1;
}

class 
EnumTwo extends SplEnum
{
    const 
__default 2;
}

class 
EnumThree extends SplEnum
{
    const 
__default 3;
}

$enumOne   = new EnumOne();
$enumTwo   = new EnumTwo();
$enumThree = new EnumThree();


echo 
'Enum one  : ' $enumOne   PHP_EOL;
echo 
'Enum two  : ' $enumTwo   PHP_EOL;
echo 
'Enum three: ' $enumThree PHP_EOL;
?>

El resultado del ejemplo sería:

Enum one  : 1
Enum two  : 2
Enum three: 3



add a note add a note User Contributed Notes SplEnum::__construct
No Such Alias 30-Jul-2010 04: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