note 116325 added to class.splenum

From: Date: Fri, 12 Dec 2014 20:11:13 +0000
Subject: note 116325 added to class.splenum
Groups: php.notes 
Request: Send a blank email to php-notes+get-201137@lists.php.net to get a copy of this message
SplEnum is a nice start for enumerated type support as an extension (making enum a part of the language would be much better), but it lacks a lot of features that enums have in other languages. I needed functionality like the hasKey() method supported by Java. I extended the class as SplEnumPlus and used that subclass in my code as follows: <?php class SplEnumPlus extends SplEnum { static function hasKey($key) { $foundKey = false; try { $enumClassName = get_called_class(); new $enumClassName($key); $foundKey = true; } finally { return $foundKey; } } } class Fruit extends SplEnumPlus { const APPLE = 1; const ORANGE = 2; } echo (Fruit::hasKey(Fruit::APPLE) ? 'yes' : 'no') . PHP_EOL; // yes echo (Fruit::hasKey('banana') ? 'yes' : 'no') . PHP_EOL; // no ?> Other useful features, like reverse value-to-key lookups, could be done in this way. It would be helpful if this and other useful functionality were made part of SplEnum. ---- Server IP: 72.52.91.14 Probable Submitter: 35.2.248.62 ---- Manual Page -- http://php.net/manual/en/class.splenum.php Edit -- https://master.php.net/note/edit/116325 Del: integrated -- https://master.php.net/note/delete/116325/integrated Del: useless -- https://master.php.net/note/delete/116325/useless Del: bad code -- https://master.php.net/note/delete/116325/bad+code Del: spam -- https://master.php.net/note/delete/116325/spam Del: non-english -- https://master.php.net/note/delete/116325/non-english Del: in docs -- https://master.php.net/note/delete/116325/in+docs Del: other reasons-- https://master.php.net/note/delete/116325 Reject -- https://master.php.net/note/reject/116325 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201137) next »