PHP中的完美枚举
最近,我为php中的枚举提出了以下解决方案:PHP中的完美枚举,php,Php,最近,我为php中的枚举提出了以下解决方案: class Enum implements Iterator { private $vars = array(); private $keys = array(); private $currentPosition = 0; public function __construct() { } public function current()
class Enum implements Iterator {
private $vars = array();
private $keys = array();
private $currentPosition = 0;
public function __construct() {
}
public function current() {
return $this->vars[$this->keys[$this->currentPosition]];
}
public function key() {
return $this->keys[$this->currentPosition];
}
public function next() {
$this->currentPosition++;
}
public function rewind() {
$this->currentPosition = 0;
$reflection = new ReflectionClass(get_class($this));
$this->vars = $reflection->getConstants();
$this->keys = array_keys($this->vars);
}
public function valid() {
return $this->currentPosition < count($this->vars);
}
}
它工作得非常完美,我得到了IDE提示,我可以遍历我所有的枚举,但我想我错过了一些有用的枚举特性。
所以我的问题是:我可以添加哪些功能来更多地使用枚举或使其更实用?我认为您可以实现ArrayAccess和Countable
class Enum implements ArrayAccess, Countable, Iterator {
(官方)以及
class Enum implements ArrayAccess, Countable, Iterator {