Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
З.Ы. А вот когда выйдет релиз PHP5.3, то можно будет добавить к класс Enum метод:
public static method asArray() {}
$ColorReflection = new ReflectionClass("Enum_Colors");
printf("<pre>%s«, print_r($ColorReflection->getConstants(), TRUE));$ColorReflection = new ReflectionClass(get_called_class());
final class enum
{
private $something;
public function __construct($val)
{
$this->something = $val;
}
public function eget($index='')
{
if($index=='') return $this->something;
if(! isset($this->something[$index])) return NULL;
return $this->something[$index];
}
}
<?
$array = array(«red»=>«#FF0000», «green»=>«#00FF00», «blue»=>«#0000FF»);
$enum = new enum($array);
echo $enum->eget(«red»);
?>
обсолютно не нужен $class_name = get_class( $this );
<?php
abstract class Color
{
protected $value;
public function getValue()
{
return $this->value;
}
}
final class Color_Red extends Color
{
protected $value = 'f00';
}
.....
....function setColor(Color $color)
class Enum_Colors extends Enum {
const RED = 0xFF0000;
const GREEN = 0x00FF00;
const BLUE = 0x0000FF;
}
function something(Enum_Colors $color)
{
switch ((string)$color) {
case Enum_Colors::RED:
echo 'red';
break;
case Enum_Colors::BLUE:
echo 'blue';
break;
case Enum_Colors::GREEN:
echo 'green';
break;
default:
echo 'unknown color';
break;
}
}
something(new Enum_Colors('RED'));
abstract class Enum {
static public function __callStatic($name, $arguments){
return new static(constant(get_called_class() . '::' . $name)); // не нужно проверять, php сам кинет ошибку.
}
private $current_val;
private function __construct($value) {
$this->current_val = $value;
}
final public function __toString() {
return $this->current_val;
}
}
Enum_Colors::, выбрать константу и дописать скобочки Enum_Colors::RED();
Перечисления в PHP