定制 codinc/phpenum 二次开发

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

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

codinc/phpenum

Composer 安装命令:

composer require codinc/phpenum

包简介

A simple PHP Enum implementation, as an alternative to SplEnum

README 文档

README

A simple PHP Enum implementation, as an alternative to SplEnum, allowing typesafety for values belonging to an enum.

Installation

composer require codinc/phpenum

Usage

The abstract class supports extension by concrete classes that define constants as valid values.

use Codinc\Type\Enum;

class MyEnum extends Enum
{
    const PERSONAL = 'personal';
    const TEAM = 'team';
}

Of course, the biggest advantage of an enum is to allow typehinting and built-in safety on methods on your set of values.

public function doAnAction(MyEnum $value)
{
    // Can only contain PERSONAL or TEAM
}

The biggest advantage and difference of this implementation is that it is allowing native strict comparison. The Enum keeps track of the instantiated objects to ensure only one instance can be used.

MyEnum::PERSONAL() === MyEnum::PERSONAL(); // === true

It is supported to call the constant as a method without added overhead. In case you do desire the benefits of autocompletion, or desire to protect your constants, you can still define the public static methods yourself and call load on the Enum.

use Codinc\Type\Enum;

class MyEnum extends Enum
{
    const PERSONAL = 'personal';
    const TEAM = 'team';
    const WORLD = 'world';
    private const COMPANY = 'company';
    
    public static function WORLD()
    {
        return self::load(self::WORLD);
    }
    
    public static function COMPANY()
    {
        return self::load(self::COMPANY);
    }
}
$personal = MyEnum::PERSONAL();
$world = MyEnum::WORLD();

Unsupported values will throw an \InvalidArgumentException.

MyEnum::load('personal'); // === MyEnum::PERSONAL()
MyEnum::load('made_up'); // throws \InvalidArgumentException
MyEnum::load($repository->fetchColumn($column));

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2018-05-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固