klamius/php-enum
Composer 安装命令:
composer require klamius/php-enum
包简介
A PHP library providing a way to work with enumeration
README 文档
README
Installation
by using composer
composer require klamius/php-enum
Philosophie
This library give an easier way to emulate and create enumeration objects natively in PHP and be replacement to
SplEnum which is not integrated directly to PHP.
In our daily usage we deal with many enums (all domain entity states, months, genders, etc. ) and we get into dilemma, should it be Class member, constants or Interface constants and so on. and then we treat it as a scalar value which most of time can't be validated or type hinted.
So using an enum instead of constants provides the following advantages:
- type hint:
function setState(OrderStateEnum $state) {
Declaration
use Klamius\Enum\Enum; /** * GenderEnum enum */ class Gender extends Enum { const MALE = 'male'; const FEMALE = 'female'; }
Usage
class User { /** * @var Gender */ private $gender; function setGender(Gender $gender) { $this->gender = $gender; } } $gender = new Gender(Gender::MALE); $user->setGender($gender); //or echo $gender;
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-22