desmart/php-enum
Composer 安装命令:
composer require desmart/php-enum
包简介
Strongly-typed enum objects for PHP
README 文档
README
Package provides a strongly typed enums for PHP.
Enumerationclass name is used on purpose, to avoid potential issues. There will be native enums in PHP 8.1 🎉
There are other great packages implementing enums for PHP. Consider checking them out!
Installation
To install the package via Composer, simply run the following command:
composer require desmart/php-enum
Usage
Example enum definition:
/** * @method static Character good() * @method static Character evil() * @method static Character sometimesGoodSometimesEvil() */ class Character extends Enumeration { const GOOD = 'good'; const EVIL = 'evil'; const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_or_evil'; }
Create an enum object
$enum = Character::good(); // or Character::fromName('good')
In general, named constructor method name should be a constant name in camelCase.
Few exceptions to this rule are allowed. See test file.
Create an enum object based on available constants' values
$enum = Character::fromValue('sometimes_good_or_evil');
This approach is very convenient when enum objects are created from eg. database.
Comparing enums
Character::good()->equals(Character::fromName('good')); // true Character::good()->equals(Character::evil()); // false Character::good()->equals(OtherEnum::someValue()); // false
Changelog
Please see CHANGELOG for more information what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 464
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-03