我知道PHP还没有本地枚举。但是我已经习惯了来自Java世界的它们。我喜欢使用枚举来提供ide的自动补全功能能够理解的预定义值。
常量可以解决这个问题,但是存在名称空间冲突问题,而且(实际上是因为)它们是全局的。数组没有名称空间的问题,但是它们太模糊了,它们可以在运行时被覆盖,而且ide很少知道如何在没有额外的静态分析注释或属性的情况下自动填充它们的键。
你有什么常用的解决方案/变通办法吗?有人记得PHP的人对枚举有什么想法或决定吗?
我知道PHP还没有本地枚举。但是我已经习惯了来自Java世界的它们。我喜欢使用枚举来提供ide的自动补全功能能够理解的预定义值。
常量可以解决这个问题,但是存在名称空间冲突问题,而且(实际上是因为)它们是全局的。数组没有名称空间的问题,但是它们太模糊了,它们可以在运行时被覆盖,而且ide很少知道如何在没有额外的静态分析注释或属性的情况下自动填充它们的键。
你有什么常用的解决方案/变通办法吗?有人记得PHP的人对枚举有什么想法或决定吗?
当前回答
现在您可以使用The脾脏类来原生构建它。根据官方文件。
脾提供了模拟和创建枚举对象的能力 原生的PHP。
<?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;
}
?>
请注意,这是一个必须安装的扩展,但默认情况下不可用。在PHP网站上描述的特殊类型下。上面的示例取自PHP站点。
其他回答
我知道这是一个旧线程,然而没有一个我所见过的解决方法真的看起来像枚举,因为几乎所有的解决方法都需要你手动分配值给枚举项,或者它需要你传递一个枚举键数组到一个函数。所以我创造了自己的解决方案。
要使用我的解决方案创建枚举类,可以简单地扩展下面的enum类,创建一堆静态变量(不需要初始化它们),并在枚举类定义的下面调用yourEnumClass::init()。
edit: This only works in php >= 5.3, but it can probably be modified to work in older versions as well /** * A base class for enums. * * This class can be used as a base class for enums. * It can be used to create regular enums (incremental indices), but it can also be used to create binary flag values. * To create an enum class you can simply extend this class, and make a call to <yourEnumClass>::init() before you use the enum. * Preferably this call is made directly after the class declaration. * Example usages: * DaysOfTheWeek.class.php * abstract class DaysOfTheWeek extends Enum{ * static $MONDAY = 1; * static $TUESDAY; * static $WEDNESDAY; * static $THURSDAY; * static $FRIDAY; * static $SATURDAY; * static $SUNDAY; * } * DaysOfTheWeek::init(); * * example.php * require_once("DaysOfTheWeek.class.php"); * $today = date('N'); * if ($today == DaysOfTheWeek::$SUNDAY || $today == DaysOfTheWeek::$SATURDAY) * echo "It's weekend!"; * * Flags.class.php * abstract class Flags extends Enum{ * static $FLAG_1; * static $FLAG_2; * static $FLAG_3; * } * Flags::init(Enum::$BINARY_FLAG); * * example2.php * require_once("Flags.class.php"); * $flags = Flags::$FLAG_1 | Flags::$FLAG_2; * if ($flags & Flags::$FLAG_1) * echo "Flag_1 is set"; * * @author Tiddo Langerak */ abstract class Enum{ static $BINARY_FLAG = 1; /** * This function must be called to initialize the enumeration! * * @param bool $flags If the USE_BINARY flag is provided, the enum values will be binary flag values. Default: no flags set. */ public static function init($flags = 0){ //First, we want to get a list of all static properties of the enum class. We'll use the ReflectionClass for this. $enum = get_called_class(); $ref = new ReflectionClass($enum); $items = $ref->getStaticProperties(); //Now we can start assigning values to the items. if ($flags & self::$BINARY_FLAG){ //If we want binary flag values, our first value should be 1. $value = 1; //Now we can set the values for all items. foreach ($items as $key=>$item){ if (!isset($item)){ //If no value is set manually, we should set it. $enum::$$key = $value; //And we need to calculate the new value $value *= 2; } else { //If there was already a value set, we will continue starting from that value, but only if that was a valid binary flag value. //Otherwise, we will just skip this item. if ($key != 0 && ($key & ($key - 1) == 0)) $value = 2 * $item; } } } else { //If we want to use regular indices, we'll start with index 0. $value = 0; //Now we can set the values for all items. foreach ($items as $key=>$item){ if (!isset($item)){ //If no value is set manually, we should set it, and increment the value for the next item. $enum::$$key = $value; $value++; } else { //If a value was already set, we'll continue from that value. $value = $item+1; } } } } }
现在您可以使用The脾脏类来原生构建它。根据官方文件。
脾提供了模拟和创建枚举对象的能力 原生的PHP。
<?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;
}
?>
请注意,这是一个必须安装的扩展,但默认情况下不可用。在PHP网站上描述的特殊类型下。上面的示例取自PHP站点。
// My Enumeration Class
class Enum
{
protected $m_actions = array();
public function __construct($actions)
{
$this->init($actions);
}
public function init($actions)
{
$this->m_actions = array();
for($i = 0; $i < count($actions); ++$i)
{
$this->m_actions[$actions[$i]] = ($i + 1);
define($actions[$i], ($i + 1));
}
}
public function toString($index)
{
$keys = array_keys($this->m_actions);
for($i = 0; $i < count($keys); ++$i)
{
if($this->m_actions[$keys[$i]] == $index)
{
return $keys[$i];
}
}
return "undefined";
}
public function fromString($str)
{
return $this->m_actions[$str];
}
}
// Enumeration creation
$actions = new Enum(array("CREATE", "READ", "UPDATE", "DELETE"));
// Examples
print($action_objects->toString(DELETE));
print($action_objects->fromString("DELETE"));
if($action_objects->fromString($_POST["myAction"]) == CREATE)
{
print("CREATE");
}
基于此要点,所有枚举的基类:
abstract class Enum {
protected $val;
protected function __construct($arg) {
$this->val = $arg;
}
public function __toString() {
return $this->val;
}
public function __set($arg1, $arg2) {
throw new Exception("enum does not have property");
}
public function __get($arg1) {
throw new Exception("enum does not have property");
}
// not really needed
public function __call($arg1, $arg2) {
throw new Exception("enum does not have method");
}
// not really needed
static public function __callStatic($arg1, $arg2) {
throw new Exception("enum does not have static method");
}
}
你的枚举:
final class MyEnum extends Enum {
static public function val1() {
return new self("val1");
}
static public function val2() {
return new self("val2");
}
static public function val3() {
return new self("val3");
}
}
测试:
$a = MyEnum::val1();
echo "1.the enum value is '$a'\n";
function consumeMyEnum(MyEnum $arg) {
return "2.the return value is '$arg'\n";
}
echo consumeMyEnum($a);
$version = explode(".", PHP_VERSION);
if ($version[0] >= 7) {
try {
echo consumeMyEnum("val1");
} catch (TypeError $e) {
echo "3.passing argument error happens (PHP 7.0 and above)\n";
}
}
echo ($a == MyEnum::val1()) ? "4.same\n" : "4.different\n";
echo ($a == MyEnum::val2()) ? "5.same\n" : "5.different\n";
$b = MyEnum::val1();
echo ($a == $b) ? "6.same\n" : "6.different\n";
echo ($a === $b) ? "7.same\n" : "7.different\n";
$c = MyEnum::val2();
echo ($a == $c) ? "8.same\n" : "8.different\n";
echo ($a === $c) ? "9.same\n" : "9.different\n";
switch ($c) {
case MyEnum::val1(): echo "10.case of 1st\n"; break;
case MyEnum::val2(): echo "11.case of 2nd\n"; break;
case MyEnum::val3(): echo "12.case of 3rd\n"; break;
}
try {
$a->prop = 10;
} catch (Exception $e) {
echo "13.set property error happens\n";
}
try {
echo $a->prop;
} catch (Exception $e) {
echo "14.get property error happens\n";
}
try {
echo $a->meth();
} catch (Exception $e) {
echo "15.method call error happens\n";
}
try {
echo MyEnum::meth();
} catch (Exception $e) {
echo "16.static method call error happens\n";
}
class Ordinary {}
echo $a instanceof MyEnum ? "17.MyEnum instance\n" : "17.not MyEnum instance\n";
echo $a instanceof Enum ? "18.Enum instance\n" : "18.not Enum instance\n";
echo $a instanceof Ordinary ? "19.Ordinary instance\n" : "19.not Ordinary instance\n";
在网上试试:沙盒
一个不使用反射的更简单、更轻的版本:
abstract class enum {
private function __construct() {}
static function has($const) {
$name = get_called_class();
return defined("$name::$const");
}
static function value($const) {
$name = get_called_class();
return defined("$name::$const")? constant("$name::$const") : false;
}
}
用法:
class requestFormat extends enum { const HTML = 1; const JSON = 2; const XML = 3; const FORM = 4; }
echo requestFormat::value('JSON'); // 2
echo requestFormat::has('JSON'); // true
这提供了常量的优势,也允许检查它们的有效性,但它缺乏更复杂的解决方案所提供的其他花哨功能,更明显的是无法检查值的反向(在上面的例子中,你不能检查'2'是否是一个有效值)