定制 aldemeery/enum-polyfill 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aldemeery/enum-polyfill

最新稳定版本:v1.0.2

Composer 安装命令:

composer require aldemeery/enum-polyfill

包简介

A Polyfill for the SplEnum type in PHP

README 文档

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.

统计信息

  • 总下载量: 53.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固