Enum support
While PHP itself does not offer the possibility of enums like it may be known from Java or C#, Stubbles offer support to emulate them. The implementation is a bit hackish in point of how to create the enum instances and that they are changable due to some missing features in PHP, but used correct and with a bit discipline they have a lot of advantages regarding to code clearity, code reuse and maintenance.
Create your own Enum
To create your own Enum you have to extend net::stubbles::lang::stubEnum:
<?php class ReportCellValueType extends stubEnum { /** * type of the cell value: string * * @var ReportCellValueType */ public static $STRING; /** * type of the cell value: integer * * @var ReportCellValueType */ public static $INT; /** * type of the cell value: percentage * * @var ReportCellValueType */ public static $PERCENTAGE; /** * static initializer */ public static function __static() { self::$STRING = new self('STRING', create_function('$value', 'return (string) $value;')); self::$INT = new self('INT', create_function('$value', "return number_format(\$value, 0, ',', '.');")); self::$PERCENTAGE = new self('PERCENTAGE', create_function('$value', "return number_format(\$value, 4, ',', '.') . ' %';")); } /** * format the value * * @param scalar $value * @return scalar */ public function format($value) { $func = $this->value; return $func($value); } // implementations of __sleep() and __wakeup() to restore the lambda function after unserializing } ?>
You need to declare public static properties which will hold the instances of the enum class. Additionally you may add other methods that perform operations specific for the enum. Finally a public static function __static() is required which takes care of initializing the static properties. This method is called when the class is loaded via the class loader, see Static initializing for more information on this.
Usage of enums
The instances of the ReportCellValueType declared above may now be used as follows:
<?php echo ReportCellValueType::$PERCENTAGE->format(5); // displays "5.000 %" ?>
Methods available in all enums
name()
Returns the name of the enum.
value()
Returns the value of the enum.
Static methods of stubEnum
All methods listed below throw a net::stubbles::lang::exceptions::stubIllegalArgumentException in case $enum does not denote a net::stubbles::lang::stubEnum child class.
net::stubbles::lang::stubEnum::forName(ReflectionClass $enum, $name)
If $enum denotes a stubEnum child class the method returns the instance associated with $name.
net::stubbles::lang::stubEnum::forValue(ReflectionClass $enum, $value)
If $enum denotes a stubEnum child class the method returns the instance associated with $value.
net::stubbles::lang::stubEnum::instances(ReflectionClass $enum)
If $enum denotes a stubEnum child class the method returns a list of all instances of this enum.
net::stubbles::lang::stubEnum::namesOf(ReflectionClass $enum)
If $enum denotes a stubEnum child class the method returns a list of all names of this enum.
net::stubbles::lang::stubEnum::valuesOf(ReflectionClass $enum)
If $enum denotes a stubEnum child class the method returns a list of all values of this enum.
