On 2/18/2011 9:28 PM, Ben Schmidt wrote:
I did some research on methods in enums and discovered that there is
some usefulness to the idea - I wouldn't go so far as to say that they
would be needed, but C#, for example, allows you to create extension
methods for enums and MSDN has a decent real-world example of its use.
http://msdn.microsoft.com/en-us/library/bb383974.aspx
It's a pretty big new concept that would need to be introduced if this
was desired: extension methods.
I think it's overkill myself. Something like this is fine:
class Grade {
enum {
A_PLUS,
A,
B_PLUS,
B,
...,
FAIL,
NOT_AVAILABLE,
}
public static function passing($grade) {
return $grade>=self::D;
}
}
$grade=Grade::B;
echo Grade::passing($grade)?"passing":"not passing";
Shouldn't that be:
public static function passing($grade) {
-return $grade>=self::D;
+return $grade<=self::D;
The passing grades all appear before D in the enum, and I expect those
to have lower values.
Also, I would probably put NOT_AVAILABLE first, since it's underlying
value is 0. But then the programmer isn't supposed to consider the
underlying values...
Rick
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php