why not supporting methods for enum values?
developers will need that, and by providing type hinting, they will just
create the logic somewhere else...
why would developers need this? can you elaborate with some real-life
scenario?
I thought enums are just strong-typed constants
I think this way, too.
Strings are not objects in PHP, and you can't call methods on them. It
makes sense for enum constants to behave the same way in PHP and not
have callable methods.
Think on any finite set of elements that cannot be represented with integers
because they don't hold enough data... or because the repeated values.
An extremely example could be the "Periodic Table", finite set of elements,
where each element holds a lot of information.
function print_element(Element $e) {
echo $e->weight(), PHP_EOL,
$e->density(), PHP_EOL,
$e->protons(), PHP_EOL,
$e->neutrons();
}
print_element(Element:Hydrogen);
print_element(Element:Argon);
print_element(Element:Iron);
How do you implement this with just string=>integer define or constants?
so you need classes.. do they fit well? I don't think so
$h = new Hydrogen(); // What would mean this?
$h = Element::Hydrogen(); // probably with a static method
$h = Element::Hydrogen; // class constant? lack of objects support
I think this is good enough:
abstract class Element {
enum {
Hydrogen,
Argon,
Iron
}
public static function weight(Element $e) {
...
}
...
}
function print_element(Element $e) {
echo Element::weight($e), PHP_EOL,
Element::density($e), PHP_EOL,
Element::protons($e), PHP_EOL,
Element::neutrons($e);
}
print_element(Element::Hydrogen);
print_element(Element::Argon);
print_element(Element::Iron);
Ben.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php