bonndan/phpenums 问题修复 & 功能扩展

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

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

bonndan/phpenums

Composer 安装命令:

composer require bonndan/phpenums

包简介

An experimental Enum implementation for PHP7

README 文档

README

Build Status Minimum PHP Version

Yet another implementation of Enums in PHP. The difference to most other implementations is that the implemetation is very close to Java. Static properties are used, but like in Java they are real instances of the Enum providing all methods etc:

class Fruit extends \Pava\Enum
{
    /**
     * @var Fruit
     */
    public static $APPLE;

    /**
     * @var Fruit
     */
    public static $BANANA;
}

So you can use static typing:

$mix = function (Fruit $a, Fruit $b) {};

$blend = $mix(Fruit::$APPLE, Fruit::$BANANA);

$grow = function () : Fruit { return Fruit::$APPLE };

assert(Fruit::$APPLE instanceof Fruit);

What are the drawbacks?

  • Each enum has to be registered (unless static constructors arrive in PHP). The reason is that static properties can only have scalar values as default.
//class Fruit ...

Pava\register(Fruit::class);
  • You need to annotate all static props in order to get your IDE working with it, but at least this works.
  • No default values usable. At "compile time" $BANANA is not yet an instance. However you can use null as default value.
//not possible
$a = function (Fruit $a = Fruit::$BANANA) {};
  • Use/import is not possible, i.e. you cannot just write $BANANA.

Advanced use

Each enum instance can obtain its own properties by implementing the magic invoke method.

class Fruit extends \Pava\Enum
{
    private $color;

    /**
     * @var Fruit
     */
    public static $APPLE;

    /**
     * @var Fruit
     */
    public static $BANANA;

    public function color() : string
    {
        return $this->color;
    }

    public function __invoke()
    {
        if ($this->name() == 'BANANA')
            $this->color = 'yellow';
        if ($this->name() == 'APPLE')
            $this->color = 'green';
    }
}
Pava\register(Fruit::class);

echo 'The apple is ' . Fruit::$APPLE->color();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-01-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固