-
Notifications
You must be signed in to change notification settings - Fork 36
Get a list of all enumerator instances of the called enumeration type #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Added static method which helps get a list of all enumeration instances of actual implementation
change array notation to the old way
* | ||
* @return Enum[] | ||
*/ | ||
public static function getAll() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final public static function getInstances()
{
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class()));
}
Thoughts ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah!
I'll mention here:
- using callable
'self::getByName'
take execution time for 8% ca. - using
foreach
is faster asarray_map
not so wild (-2% - 5% on several versions of PHP and HHVM) - name of method maybe
listEnum
orgetListEnumeration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like getInstances
or getEnumerators
much more as it's consistent with the name getConstants
and the term list
doesn't help and enum
is ambiguous: enumeration type vs. enumerator element. I'm going to fix up the variable names of $enum
as well.
I have benchmarked 3 versions but my version is a little bit faster :)
Tested an enumeration with 26 elements or PHP-5.5.6:
final public static function getEnumerators1()
{
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class())));
}
final public static function getEnumerators2()
{
$enumerators = array_keys(self::detectConstants(get_called_class()));
foreach ($enumerators as & $entry) {
$entry = self::getByName($entry);
}
return $enumerators;
}
final public static function getEnumerators3()
{
$enums = array();
foreach (array_keys(self::detectConstants(get_called_class())) as $v) {
$enums[] = self::getByName($v);
}
return $enums;
}
class MyEnum extends \MabeEnum\Enum {
const A = 'a';
const B = 'b';
const C = 'c';
const D = 'd';
const E = 'e';
const F = 'f';
const G = 'g';
const H = 'h';
const I = 'i';
const J = 'j';
const K = 'k';
const L = 'l';
const M = 'm';
const N = 'n';
const O = 'o';
const P = 'p';
const Q = 'q';
const R = 'r';
const S = 's';
const T = 't';
const U = 'u';
const V = 'v';
const W = 'w';
const X = 'x';
const Y = 'y';
const Z = 'z';
}
MyEnum::getConstants();
echo 'getEnumerators1(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
MyEnum::getEnumerators1();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;
echo 'getEnumerators2(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
MyEnum::getEnumerators2();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;
echo 'getEnumerators3(): ';
$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
MyEnum::getEnumerators3();
}
$end = microtime(true);
echo ($end - $start) . PHP_EOL;
php bench.php
getEnumerators1(): 0.53819012641907
getEnumerators2(): 0.60042691230774
getEnumerators3(): 0.60367202758789
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@marc-mabe thx!
Is it possible to have this in integrated until 2.0.0? Also: What's the time schedule for 2.0.0? |
@prolic I'll release the first beta of 2.0.0 the next week(s) and the first stable ~2-3 weeks later. |
No leaving this for 2.0.0 is okay (at least for me). But I have nothing against, of you want to release 1.x. An additional beta should not be needed. Giving it available for testing in master is sufficient (just like we do it in ZF2). I am just curious for the 2.0.0 release, as I have some classes integrating php-enum with doctrine 2, and the "real bitset" implementation would lead to some minor refactoring, making the code much easier on my side. I am planning to release it, as soon as 2.0.0 is available. |
By the way: My vote is for "getEnumerators" as the method name ;) Mhh.. can I vote? lol |
Added static method which helps get a list of all enumeration instances of actual implementation