aldemeery/enum-polyfill

A Polyfill for the SplEnum type in PHP

Installs: 32 926

Dependents: 0

Suggesters: 0

Security: 0

Stars: 12

Watchers: 1

Forks: 0

Open Issues: 0

Type:snippet

v1.0.1 2020-12-23 01:51 UTC

This package is auto-updated.

Last update: 2024-04-23 09:12:44 UTC


README

This is a polyfill for the SplEnum type from the PECL extension, so you don't have to download and install the extension on you server.

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

Example

From the php.net documentation.

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

6
Value '13' is not part of the enum Month

Class sypnosis

abstract class SplEnum
{
    /* Constants */
    const NULL __default = NULL ;

    /* Methods */
    SplType::__construct ([ mixed $initial_value ] )

    public array getConstList ([ bool $include_default = FALSE ] )

}

Predefined Constants

/* The default value of the enum */
SplEnum::__default

Methods

SplType::__construct

The SplEnum constructor.

SplType::__construct ([ mixed $initial_value ] )

Parameters

initial_value The initial value of the enum.

Errors/Exceptions

Throws an UnexpectedValueException if incompatible type is given.

SplEnum::getConstList

Returns all the consts as an array

public array SplEnum::getConstList ([ bool $include_default = false ] )

Parameters

include_default Whether to include __default property.

Author