您好,欢迎访问一九零五行业门户网

PHP中Enum的使用技巧

本篇文章主要介绍php中enum的使用技巧,感兴趣的朋友参考下,希望对大家有所帮助。
php其实有enum类库的,需要安装perl扩展,所以不是php的标准扩展,因此代码的实现需要运行的php环境支持。
(1)扩展类库splenum类。该类的摘要如下:
splenum extends spltype {/* constants */const null __default = null ;/* 方法 */public array getconstlist ([ bool $include_default = false ] )/* 继承的方法 */spltype::__construct ([ mixed $initial_value [, bool $strict ]] )}
使用示例:
<?phpclass month extends splenum { const __default = self::january; const january = 1; const february = 2; const march = 3; const april = 4; const may = 5; const june = 6; const july = 7; const august = 8; const september = 9; const october = 10; const november = 11; const december = 12;}echo new month(month::june) . php_eol;try { new month(13);} catch (unexpectedvalueexception $uve) { echo $uve->getmessage() . php_eol;}?>
输出结果:
6value not a const in enum month
(2)自定义的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 <msufflaj32@gmail.com&gt * @link http://php4every1.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 = 1; const __default = self::by;}var_dump(new myenum(myenum::hi));var_dump(new myenum(myenum::by));//use __defaultvar_dump(new myenum());try { new myenum("i don't exist");} catch (unexpectedvalueexception $e) { var_dump($e->getmessage());}
输出结果如下:
object(myenum)#1 (2) { ["value":"enum":private]=> string(2) "hi" ["strict":"enum":private]=> bool(true)}object(myenum)#1 (2) { ["value":"enum":private]=> string(2) "by" ["strict":"enum":private]=> bool(true)}object(myenum)#1 (2) { ["value":"enum":private]=> string(2) "by" ["strict":"enum":private]=> bool(true)}string(27) "value is not in enum myenum"
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php实现当前页面点击下载文件的方法
php使用ftp远程上传文件类实例详解
php的api数据接口书写实例详解
以上就是php中enum的使用技巧的详细内容。
其它类似信息

推荐信息