-
Notifications
You must be signed in to change notification settings - Fork 0
Docs Enums
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.
To create your own Enum you have to extend net::stubbles::lang::stubEnum:
#php <?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 <u>sleep() and </u>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.
The instances of the ReportCellValueType declared above may now be used as follows:
#php <?php echo ReportCellValueType::$PERCENTAGE->format(5); // displays "5.000 %" ?>
Returns the name of the enum.
Returns the value of the enum.
All methods listed below throw a net::stubbles::lang::exceptions::stubIllegalArgumentException in case $enum does not denote a net::stubbles::lang::stubEnum child class.
If $enum denotes a stubEnum child class the method returns the instance associated with $name.
If $enum denotes a stubEnum child class the method returns the instance associated with $value.
If $enum denotes a stubEnum child class the method returns a list of all instances of this enum.
If $enum denotes a stubEnum child class the method returns a list of all names of this enum.
If $enum denotes a stubEnum child class the method returns a list of all values of this enum.
Each of those static methods should be called directly on the class which extends the stubEnum class:
$instanceList = ReportCellValueType::instances(); $stringCellValueType = ReportCellValueType::forName('STRING');
Returns the instance associated with $name.
Returns the instance associated with $value.
Returns a list of all instances of this enum.
Returns a list of all names of this enum.
Returns a list of all values of this enum.