note 97053 deleted from language.oop5 by danbrown

From: Date: Tue, 30 Mar 2010 14:29:07 +0000
Subject: note 97053 deleted from language.oop5 by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-167533@lists.php.net to get a copy of this message
Note Submitter: Hayley Watson ---- I have found the classes posted by redrik at gmail dot com useful; I added an additional method to the base Enum class, overriding it for FlagsEnum: <?php class Enum { // ... // Returns this Enum's name corresponding to the given value, // or false if there is no such name. public function name($value) { return array_search($value, $this->self); } } class FlagsEnum extends Enum { // ... // Returns the flag names corresponding to this bit pattern, // separated by "|". // // If there are bits set in the pattern that do not have names // then they are returned as a literal (integer) mask appended to the // list of known flags public function name($value) { $return = array(); foreach(array_reverse($this->self) as $name=>$bit) { if($bit & $value) { $value &= ~$bit; $return[] = $name; } } if($value) { $return[] = $value; } return join("|", $return); } } ?> Example usage: <?php echo $eFruits->name(1),',',$eFruits->name(9),',',$eFruits->name(3),"\n"; echo $eBeers->name(25),"\n"; echo $eFlags->name(7),"\n",$eFlags->name(42),"\n"; echo "\n"; echo $eFruits->{$eFruits->name(3)}; ?>

« previous php.notes (#167533) next »