Advertisement
Guest User

PHP enumerated string values

a guest
Jul 18th, 2011
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.38 KB | None | 0 0
  1. class PageSpecial extends AEnum
  2. {
  3.     static final function home()
  4.     {
  5.         return self::get();
  6.     }
  7.  
  8.     static final function conditions()
  9.     {
  10.         return self::get();
  11.     }
  12. }
  13.  
  14. class Month extends AEnum
  15. {
  16.     static final function JANUARY()
  17.     {
  18.         return self::get(1);
  19.     }
  20.    
  21.     static final function FEBRUARY()
  22.     {
  23.         return self::get(2);
  24.     }
  25. }
  26.  
  27. class Color extends AEnum
  28. {
  29.     private $r, $g, $b;
  30.    
  31.     protected function init($r, $g, $b)
  32.     {
  33.         $this->r = $r;
  34.         $this->g = $g;
  35.         $this->b = $b;
  36.     }
  37.  
  38.     static final function BLACK()
  39.     {
  40.         return self::get(0, 0, 0);
  41.     }
  42.  
  43.     static final function WHITE()
  44.     {
  45.         return self::get(255, 255, 255);
  46.     }
  47. }
  48.  
  49. function linkPage(PageSpecial $special) {
  50.     echo $special;
  51. }
  52.  
  53. echo Month::FEBRUARY();
  54. linkPage(PageSpecial::CONDITIONS());
  55.  
  56. $feb = Month::FEBRUARY();
  57. var_dump($feb === Month::FEBRUARY());
  58. var_dump($feb() + 1);
  59.  
  60. var_dump(Color::WHITE());
  61. var_dump(Color::getNames());
  62.  
  63. //----------------------------------------
  64.  
  65. abstract class AEnum
  66. {
  67.     private static $instances, $initialized, $names;
  68.     private $value;
  69.  
  70.     private function __construct($value)
  71.     {
  72.         $class = get_called_class();
  73.        
  74.         if (empty(self::$initialized[$class])) {
  75.             self::$initialized[$class] = true;
  76.  
  77.             if (is_callable('static::init')) {
  78.                 $m = new \ReflectionMethod($class, 'init');
  79.  
  80.                 if (!$m->isProtected()) {
  81.                     trigger_error(
  82.                         "Method init in class $class must be protected",
  83.                         E_USER_ERROR
  84.                     );
  85.                 }
  86.             }
  87.            
  88.             static::saveNames();
  89.         }
  90.        
  91.         $this->value = (string) $value;
  92.     }
  93.  
  94.     function __toString()
  95.     {
  96.         return (string) $this->value;
  97.     }
  98.  
  99.     function __invoke()
  100.     {
  101.         return $this->value;
  102.     }
  103.  
  104.     static final function getNames()
  105.     {
  106.         static::saveNames();
  107.        
  108.         return self::$names[get_called_class()];
  109.     }
  110.    
  111.     protected static final function get()
  112.     {
  113.         $class = get_called_class();
  114.         $name = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
  115.  
  116.         if (empty(self::$instances[$class][$name])) {
  117.             self::$instances[$class][$name] = new $class(
  118.                 func_num_args() == 1 ? func_get_arg(0) : $name
  119.             );
  120.         }
  121.  
  122.         $enum = self::$instances[$class][$name];
  123.  
  124.         if (is_callable("static::init")) {
  125.             if (func_num_args()) {
  126.                 call_user_func_array(
  127.                     array($enum, 'init'), func_get_args()
  128.                 );
  129.             } else {
  130.                 $enum->init();
  131.             }
  132.         }
  133.  
  134.         return $enum;
  135.     }
  136.  
  137.     private final static function saveNames()
  138.     {
  139.         $class = get_called_class();
  140.  
  141.         if (!isset(self::$names[$class])) {
  142.             $c = new \ReflectionClass($class);
  143.             self::$names[$class] = array();
  144.  
  145.             foreach ($c->getMethods() as $m) {
  146.                 if ($m->isFinal() && $m->isStatic() &&
  147.                     $m->getDeclaringClass() == $c
  148.                 ) {
  149.                     self::$names[$class][] = strtoupper($m->getName());
  150.                 }
  151.             }
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement