本文实例讲述了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> * @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程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
thinkphp5 + ajax 使用formdata提交数据(包括文件上传) 后台返回json完整实例
网友评论