giritli/enum 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

giritli/enum

最新稳定版本:v0.1.5

Composer 安装命令:

composer require giritli/enum

包简介

Easy to use enum class.

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage

Enum

PHP lacks enums. This is an implementation of an enum type in PHP. It allows you to define your own enum types and use them as value objects.

Installation

To install this package using composer, run the following command:

composer require giritli/enum

Example usage

Create your initial class that will extend the Enum class.

final class StatusEnum extends Giritli\Enum\Enum {
    
    const draft = 'draft';
    const active = 'active';
    const archived = 'archived';
    const cancelled = 'cancelled';
    
    protected $default = self::draft;
}

It is that simple. You now have a fully fledged enum. As the enum class is meant to be a data time, it is recommended that all enum classes be made final but this is of course down to individual use case.

Example usage:

// This status defaults to draft as specified in the class
$status = new StatusEnum();


// You can instantiate an enum by value
$status = new StatusEnum('active');
$status = new StatusEnum(StatusEnum::active);


// Or by name
$status = StatusEnum::draft();


// Get the ordinal value of the enum
$status->getOrdinal(); // 0


// Get the value of the enum
$status->getValue(); // draft
echo $status;


// Get the key of the enum
$status->getKey(); // draft


// Get all values of an enum
$status->getValues();
StatusEnum::getValues();


// Get all ordinal values of an enum
$status->getOrdinals();
StatusEnum::getOrdinals();


// Get all key values of an enum
$status->getKeys();
StatusEnum::getKeys();

A key is the name of the class constant and value is the value of the class constant. Usually these values should be identical but there are cases when this is not true.

An enum once instantiated cannot have it's value changed. You will need to instantiate a new object of a different value. Enum values cannot be dynamically altered either.

How does it work?

Enum values are specified by the class constants that are defined. An enum can be instantiated by passing an enum value, or without passing any value if there is a default enum value. An enum can also be instantiated by calling the enum name as a method.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-10-26