定制 garoevans/php-enum 二次开发

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

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

garoevans/php-enum

最新稳定版本:1.2.0

Composer 安装命令:

composer require garoevans/php-enum

包简介

Convenient way to always have an Enum object available and utilise Spl Types if available.

README 文档

README

This gives us a convenient way to always have an Enum object available and utilise Spl Types if available. It does kick up a bit of a fuss in some IDEs as it sees two classes with the same name, but we know this isn't an issue as the code is fine and dandy :)

We also wrap the SplEnum class to stop IDEs thinking that the constructor parameters are necessary. Force it to act like the documentation says it should.

The replacement is so closely tied to the expected usage of the Spl Enum that you could just use the http://php.net/manual/en/class.splenum.php documentation;

use Garoevans\PhpEnum\Enum;

class Fruit extends Enum
{
    // If no value is given during object construction this value is used
    const __default = self::APPLE;
    // Our enum values
    const APPLE     = 1;
    const ORANGE    = 2;
}

$myApple  = new Fruit();
$myOrange = new Fruit(Fruit::ORANGE);
$fail     = 1;

function eat(Fruit $aFruit)
{
    if ($aFruit->is(Fruit::APPLE)) {
        echo "Eating an apple.\n";
    } else if ($aFruit->is(Fruit::ORANGE)) {
        echo "Eating an orange.\n";
    }
}

// Eating an apple.
eat($myApple);
// Eating an orange.
eat($myOrange);

// PHP Catchable fatal error:  Argument 1 passed to eat() must be an
// instance of Fruit, integer given
eat($fail);

Apart from normalizing the Spl Enum construct there are additions including a shorthand way of instantiating via a static method named the same as the desired constant. Using the Fruit class from above we can do the following;

// Eating an apple.
eat(Fruit::APPLE());

Also, the constantExists() method is available for use;

$fruit = new Fruit();
if ($fruit->constantExists("apple")) {
    echo "Apple is available.\n";
}

统计信息

  • 总下载量: 153.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 2
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固