首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

SplEnum (class)

Introduction

(PECL spl_types >= 0.1.0)

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

Class synopsis

SplEnum extends SplType {

/* Constants */

const NULL __default = null ;

/* Methods */

代码语言:javascript
复制
public array getConstList ([ bool $include_default = false ] )

/* Inherited methods */

代码语言:javascript
复制
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )

}

Predefined Constants

SplEnum::__default

Examples

Example #1 SplEnum usage example

代码语言:javascript
复制
<?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;
}
?>

The above example will output:

代码语言:javascript
复制
6
Value not a const in enum Month

Table of Contents

  • SplEnum::getConstList — Returns all consts (possible values) as an array.

← SplFloat

SplEnum::getConstList →

代码语言:txt
复制
 © 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券