承接 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.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 1
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-05