PHP中没有struct、enum这些东西,虽然万能的array用起来很爽,但写出来的代码typo问题很多、可维护性也差,需要更多的测试来辅助,而一旦加上测试的工作量是否还谈得上『爽』就是个问号了。

看了一篇研究Java enum机制的文章,文章里提到java的具体的enum其实就是一个类,重点是它的每一个枚举值也是一个enum对象。

对照着用PHP初步实现了一下。PHP的__callStatic 不支持静态属性访问,所以暂时用静态方法的形式访问枚举值 (string) Color::RED()。

<?php

interface enum
{
    
}

/**
 * @method enum RED() Description
 */
final class Color implements enum
{    
    private static $enums = 
        [
            'RED' => [255, 0, 0],
            'BLUE' => [0, 0, 255],
            'BLACK' => [0, 0, 0],
            'YELLOW' => [255, 255, 0],
            'GREEN' => [0, 255, 0]
        ];
    private static $objs = [];
    
    private $name;
    
    public static function __callStatic($name, $arguments)
    {
        if (!array_key_exists($name, static::$enums)) {
            throw new Exception('enum not existed', -1);
        }
        
        return static::valueOf($name);
    }
    
    private function setName($val): void
    {
        $this->name = $val;
    }
    
    private static function initEnum(string $name): bool
    {
        if (isset(static::$objs[$name])) {
            return true;
        }
        
        if (!array_key_exists($name, static::$enums)) {
            throw new Exception('enum not existed', -1);
        }
        
        $obj = new Color();
        $obj->setName($name);
        
        static::$objs[$name] = $obj;
        
        return true;
    }
    
    public static function values(): array
    {
        if (empty(static::$objs)) {
            foreach (array_keys(static::$enums) as $name) {
                static::initEnum($name);
            }
        }
        
        return static::$objs;
    }
    
    public static function valueOf(string $name): enum
    {
        if (!array_key_exists($name, static::$enums)) {
            throw new Exception('enum not existed', -1);
        }
        
        static::initEnum($name);
        
        return static::$objs[$name];
    }
    
    public function ordinal(): int
    {
        //TODO
        return 0;
    }
    
    /**
 * @throws ClassCastException
 */
    public function compareTo(enum $other): int
    {
        //TODO
        return -1;
    }
    
    public function equals(enum $other): bool
    {
        //TODO
        return true;
    }
    
    public function __toString()
    {
        if (!$this->name) {
            return '';
        }
        
        return '(' . implode(',', static::$enums[$this->name]) . ')';
    }
}  


echo (string) Color::RED();
echo (string) Color::valueOf('BLACK');

更多相关文章

  1. php static静态变量及方法详解
  2. 使用phpnow本地搭建Discuz!如何实现伪静态
  3. IIS 8.5 伪静态去掉index.php thinkphp 3.2.2
  4. 关于静态方法不能调用类中的非静态属性的理解
  5. php中static 静态变量和普通变量的区别
  6. 网站优化之PHPCMS如何开启伪静态
  7. 在Express中提供静态HTML文件的不同路径
  8. 如何使用django从静态文件加载静态文件?
  9. 动态更改angularjs中静态段落的颜色

随机推荐

  1. return后面的值不能为表达式吗?
  2. c语言define什么意思
  3. c语言颜色代码
  4. c++如何读取excel
  5. c语言的注释定界符是什么
  6. vb数组怎么定义
  7. c程序怎么编写x的y次方
  8. devc如何恢复默认设置
  9. mod在vb中什么意思
  10. C语言注释详解