您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php 中Enum(枚举)用法详解

2024/3/22 0:09:15发布40次查看
枚举是一个整型常数的集合, 枚举在日常生活中很常见。
例如表示星期的sunday, monday, tuesday, wednesday, thursday, friday,
saturday, 就是一个枚举。
枚举的说明与结构和联合相似, 其形式为:
enum 枚举名{(枚举值表)
标识符[=整型常数],
标识符[=整型常数],
...
标识符[=整型常数],
} 枚举变量;
如果枚举没有初始化, 即省掉=整型常数时, 则从第一个标识符开始, 依
次给标识符赋0, 1, 2, ...。但当枚举值表中的某个成员赋值后, 其后的成员按依次
加1的规则确定其值。
本文实例讲述了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 ]] ) }
使用示例:
<?php class 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; } ?>
输出结果:
6 value 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 default var_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 中enum(枚举)用法详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product