note 97053 added to language.oop5
| From: | HayleyWatson at osu1 dot php dot net | Date: | Tue, 30 Mar 2010 03:43:57 +0000 |
| Subject: | note 97053 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167519@lists.php.net to get a copy of this message | ||
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)};
?>
----
Server IP: 69.147.83.197
Probable Submitter: 125.237.144.87
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- https://master.php.net/note/edit/97053
Del: integrated -- https://master.php.net/note/delete/97053/integrated
Del: useless -- https://master.php.net/note/delete/97053/useless
Del: bad code -- https://master.php.net/note/delete/97053/bad+code
Del: spam -- https://master.php.net/note/delete/97053/spam
Del: non-english -- https://master.php.net/note/delete/97053/non-english
Del: in docs -- https://master.php.net/note/delete/97053/in+docs
Del: other reasons-- https://master.php.net/note/delete/97053
Reject -- https://master.php.net/note/reject/97053
Search -- https://master.php.net/manage/user-notes.php