note 117247 added to class.splenum

From: Date: Sat, 09 May 2015 11:16:56 +0000
Subject: note 117247 added to class.splenum
Groups: php.notes 
Request: Send a blank email to php-notes+get-202480@lists.php.net to get a copy of this message
if class SplEnum is not present, I would normally use something simple like the following: abstract class DaysOfWeek { const Sunday = 0; const Monday = 1; // etc. } $today = DaysOfWeek::Sunday; However, other use cases may require more validation of constants and values. abstract class BasicEnum { private static $constCacheArray = NULL; private function __construct(){ /* Preventing instance :) */ } private static function getConstants() { if (self::$constCacheArray == NULL) { self::$constCacheArray = []; } $calledClass = get_called_class(); if (!array_key_exists($calledClass, self::$constCacheArray)) { $reflect = new ReflectionClass($calledClass); self::$constCacheArray[$calledClass] = $reflect->getConstants(); } return self::$constCacheArray[$calledClass]; } public static function isValidName($name, $strict = false) { $constants = self::getConstants(); if ($strict) { return array_key_exists($name, $constants); } $keys = array_map('strtolower', array_keys($constants)); return in_array(strtolower($name), $keys); } public static function isValidValue($value) { $values = array_values(self::getConstants()); return in_array($value, $values, $strict = true); } } By creating a simple enum class that extends BasicEnum, you now have the ability to use methods thusly for simple input validation: abstract class DaysOfWeek extends BasicEnum { const Sunday = 0; const Monday = 1; const Tuesday = 2; const Wednesday = 3; const Thursday = 4; const Friday = 5; const Saturday = 6; } DaysOfWeek::isValidName('Humpday'); // false DaysOfWeek::isValidName('Monday'); // true DaysOfWeek::isValidName('monday'); // true DaysOfWeek::isValidName('monday', $strict = true); // false DaysOfWeek::isValidName(0); // false DaysOfWeek::isValidValue(0); // true DaysOfWeek::isValidValue(5); // true DaysOfWeek::isValidValue(7); // false DaysOfWeek::isValidValue('Friday'); // false As a side note, any time I use reflection at least once on a static/const class where the data won't change (such as in an enum), I cache the results of those reflection calls, since using fresh reflection objects each time will eventually have a noticeable performance impact (Stored in an assocciative array for multiple enums). ---- Server IP: 72.52.91.14 Probable Submitter: 122.165.98.98 ---- Manual Page -- http://php.net/manual/en/class.splenum.php Edit -- https://master.php.net/note/edit/117247 Del: integrated -- https://master.php.net/note/delete/117247/integrated Del: useless -- https://master.php.net/note/delete/117247/useless Del: bad code -- https://master.php.net/note/delete/117247/bad+code Del: spam -- https://master.php.net/note/delete/117247/spam Del: non-english -- https://master.php.net/note/delete/117247/non-english Del: in docs -- https://master.php.net/note/delete/117247/in+docs Del: other reasons-- https://master.php.net/note/delete/117247 Reject -- https://master.php.net/note/reject/117247 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202480) next »