aldemeery / enum-polyfill
A Polyfill for the SplEnum type in PHP
Installs: 40 517
Dependents: 0
Suggesters: 0
Security: 0
Stars: 12
Watchers: 1
Forks: 0
Open Issues: 0
Type:snippet
Requires
- php: >=7.1
This package is auto-updated.
Last update: 2024-10-25 11:37:41 UTC
README
Warning
PHP 8.1 introduced native Enumerations (Enums), providing a modern, more efficient approach.
This package is intended for projects that rely on SplEnum
and need backward compatibility or cannot upgrade to PHP 8.1+.
If possible, consider using PHP's native enums for improved type safety, performance, and maintainability.
This package is a polyfill for the now-unsupported SplEnum
class, originally part of the PECL SPL_Types extension.
By using this polyfill, you can emulate the SplEnum
behavior in PHP without relying on the extension, which is no longer actively maintained.
The SplEnum
class provides a way to create enumeration-like objects natively in PHP, offering a structured and type-safe way to work with predefined constant values.
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; } ?>
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.