巅云智能建站平台搭建版(创业流派版)火爆上线,毕生受权!新增:文章智能收罗+全站真静态打包+都会分站+智能小法式+不法词过滤+H5自顺应+智能链词等功效功效概况
建站专提News

PHP中Enum(列举)用法实例详解

一佰互联网站开辟设想(www.taishanly.com) 宣布日期 2020-04-27 10:44:58 阅读数: 318

本文实例报告了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> * @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开辟和域名注册及假造主机办事经历,供给的办事更是天下着名。最近几年来还整合团队上风自立开辟了可视化多用户”“3.0平台版,拖拽排版网站建造设想,轻松完成pc站、手机微网站、小法式、APP一体化全网营销网站扶植 ,已胜利的为天下上百家收集公司供给自助建站平台搭建办事。更多资讯:tags标签

相干消息more

28
03月
美国告状曾入侵SEC数据库的骇客

简介:美国证券买卖委员会(SEC)与美国法律部(DOJ)本周二控告及告状了到场EDGAR骇客事务并不妥赢利的10名嫌犯,这批嫌犯经由过程黑入ED... >>概况

14
05月
百度点击算法和哪些身分有关_巅云建站

善于发现善于总结是SEO人员必备的本质,不行能向咱们站长朋友们流露对于算规矩律的任何工具,因为这些触及到焦点手艺的工具,一旦被泄露将会对百度... >>概况

26
03月
电商小法式成电商骄子,小法式商城经营该若何玩

简介:电商类小法式在小法式全体市场表现中最具代表性,小法式电商的焦点才能是它的拉新促活才能,也便是交际裂变才能非常壮大。但是,电商类小法式免... >>概况

15
04月
高端网站扶植-263企业邮箱-邮箱经营专家

263收集通讯是国际最早起头推行专业企业邮箱办事的电信级电子邮箱办事商。263企业邮箱具备全新的理念和冲破性的功效。接纳并以Xmail为焦点... >>概况

高端网站扶植

美工统筹SEO,为企业电子商务营销助力!

德律风:

023-85725751
建站

产物

域名注册 假造主机 云办事器 企业邮局
智能建站 APP打包 微站/小法式 创业平台
网站推行 媒体营销 智能收罗 AI机械人
400德律风 短信营销 店销机械人
私家定制 流派网站