X
    Categories: PHP

PHPで擬似的に列挙体を作る

 

/**
* 汎用Enumクラス
*/
abstract class Enum
{
protected $scalar;

function __construct($value)
{
$ref = new ReflectionObject($this);
$consts = $ref->getConstants();
if (! in_array($value, $consts, true)) {
throw new InvalidArgumentException;
}
$this->scalar = $value;
}

final static function __callStatic($label, $args)
{
$class = get_called_class();
$const = constant(“$class::$label”);
return new $class($const);
}

//元の値を取り出すメソッド。
//メソッド名は好みのものに変更どうぞ
public function valueOf()
{
return $this->scalar;
}

public function __toString()
{
return (string)$this->scalar;
}
}

健巳:
Related Post