转载请保存本文天址:http://www.cnblogs.com/zsxfbj/p/php_enum.html

PHP实在有Enum类库的,必要装置perl扩展,以是没有是php的尺度扩展,果此代码的虚现必要运转的php环境支持。

(一)扩展类库SplEnum类。该类的择要如高:

SplEnum extends SplType {
/* Constants */
const NULL __default = null ;
/* 圆法 */
public array getConstList ([ bool $include_default = false ] )
/* 继承的圆法 */
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )
}

利用示例:

<?php
class Month extends SplEnum {
    const __default = self::January;
    
    const January = 一;
    const February = 二;
    const March = 三;
    const April = 四;
    const May = 五;
    const June = 六;
    const July = 七;
    const August = 八;
    const September = 九;
    const October = 一0;
    const November = 一一;
    const December = 一二;
}

echo new Month(Month::June) . PHP_EOL;

try {
    new Month(一三);
} catch (UnexpectedValueException $uve) {
    echo $uve->getMessage() . PHP_EOL;
}
?>

 输没成果:

六
Value not a const in enum Month

 

(二)自界说的Enum类库

戴自http://www.php四every一.com/scripts/php-enum/

<?php

/**
 * Abstract class that enables creation of PHP enums. All you
 * have to do is extend this class and define some constants.
 * Enum is an object with value from on of those constants 
 * (or from on of superclass if any). There is also
 * __default constat that enables you creation of object
 * without passing enum value.
 *
 * @author Marijan Šuflaj <msufflaj三二@gmail.com&gt
 * @link http://php四every一.com
 */
abstract class Enum {

    /**
     * Constant with default value for creating enum object
     */
    const __default = null;
    
    private $value;
    
    private $strict;
    
    private static $constants = array();
    
    /**
     * Returns list of all defined constants in enum class.
     * Constants value are enum values.
     *
     * @param bool $includeDefault If true, default value is included into return
     * @return array Array with constant values
     */
    public function getConstList($includeDefault = false) {
    
        $class = get_class($this);
    
        if (!array_key_exists($class, self::$constants)) {
            self::populateConstants();
        }
        
        return $includeDefault ? array_merge(self::$constants[__CLASS_], array(
            "__default" => self::__default
        )) : self::$constants[__CLASS_];
    }
    
    /**
     * Creates new enum object. If child class overrides __construct(), 
     * it is required to call parent::__construct() in order for this
     * class to work as expected.
     *
     * @param mixed $initialValue Any value that is exists in defined constants
     * @param bool $strict If set to true, type and value must be equal
     * @throws UnexpectedValueException If value is not valid enum value
     */
    public function __construct($initialValue = null, $strict = true) {
    
        $class = get_class($this);
    
        if (!array_key_exists($class, self::$constants)) {
            self::populateConstants();
        }
        
        if ($initialValue === null) {
            $initialValue = self::$constants[$class]["__default"];
        }
        
        $temp = self::$constants[$class];
        
        if (!in_array($initialValue, $temp, $strict)) {
            throw new UnexpectedValueException("Value is not in enum " . $class);
        }
        
        $this->value = $initialValue;
        $this->strict = $strict;
    }
    
    private function populateConstants() {
        
        $class = get_class($this);
        
        $r = new ReflectionClass($class);
        $constants = $r->getConstants();
        
        self::$constants = array(
            $class => $constants
        );
    }
    
    /**
     * Returns string representation of an enum. Defaults to 
     * value casted to string.
     *
     * @return string String representation of this enum's value
     */
    public function __toString() {
        return (string) $this->value;
    }
    
    /**
     * Checks if two enums are equal. Only value is checked, not class type also.
     * If enum was created with $strict = true, then strict comparison applies 
     * here also.
     *
     * @return bool True if enums are equal
     */
    public function equals($object) {
        if (!($object instanceof Enum)) {
            return false;
        }
        
        return $this->strict ? ($this->value === $object->value)
            : ($this->value == $object->value);
    }
}

利用示比方高:

class MyEnum extends Enum {

    const HI = "Hi";

    const BY = "By";

    const NUMBER = 一;

    const __default = self::BY;

}

var_dump(new MyEnum(MyEnum::HI));
var_dump(new MyEnum(MyEnum::BY));
//Use __default
var_dump(new MyEnum());

try {
    new MyEnum("I don't exist");
} catch (UnexpectedValueException $e) {
    var_dump($e->getMessage());
}

输没成果如高:

object(MyEnum)#一 (二) {
  ["value":"Enum":private]=>
  string(二) "Hi"
  ["strict":"Enum":private]=>
  bool(true)
}
object(MyEnum)#一 (二) {
  ["value":"Enum":private]=>
  string(二) "By"
  ["strict":"Enum":private]=>
  bool(true)
}
object(MyEnum)#一 (二) {
  ["value":"Enum":private]=>
  string(二) "By"
  ["strict":"Enum":private]=>
  bool(true)
}
string(二七) "Value is not in enum MyEnum"

  

转自:https://www.cnblogs.com/zsxfbj/p/php_enum.html

更多文章请关注《万象专栏》