Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Flyweight Enums

View flyweight.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<?php
abstract class MyEnum {
private static $initialized = false;
private static $values = array();
public static function initialize() {
self::$values['MyFirstValue'] = new MyFirstValue(1);
self::$values['MySecondValue'] = new MySecondValue(2);
self::$values['MyThirdValue'] = new MyThirdValue(3);
}
public static function __callStatic($name, array $arguments) {
if (isset(self::$values[$name])) {
return self::$values[$name];
} else {
return null;
}
}
private $value = 0;
private function __construct($value) {
$this->value = $value;
}
public function __toString() {
return get_class($this);
}
public function value() {
return $this->value;
}
}
final class MyFirstValue extends MyEnum {}
final class MySecondValue extends MyEnum {}
final class MyThirdValue extends MyEnum {}
MyEnum::initialize();
View flyweight.php
1 2 3 4 5 6 7 8 9 10 11
<?php
require 'flyweight.php';
$first = MyEnum::MyFirstValue();
var_dump($first instanceof MyEnum); // true
var_dump($first->value()); // 1
var_dump((string) $first); // 'MyFirstValue'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.