-
-
Notifications
You must be signed in to change notification settings - Fork 132
Change calls to method toArray() from self::toArray() to static::toArray() #21
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
I'm afraid the change to |
It is an interesting point to explore. I will do some performance test and share the results. |
Wouldn't it be better to introduce an |
It is a possibility, although in that case a lot of common logic would be lost. |
I did a performance test. You can find repository here: https://github.com/lorenzomar/php-enum-test. Results - original implementation: __constructor: Results - edited implementation: __constructor: |
Having an abstract class + an interface would cause loading 3 files instead of one :/ Even though it may sounds ridiculous I've definitely seem the performance matter (that's why I removed it from PHP-DI 5 for example), and I'm not sure it's really worth it… (the code is really simple, I'm fine with such code duplication honestly) |
Take a look at the performance tests. It seems really good. I did the test on 30.000 initializations of a class containing 300 constants. You can obtain an average difference of 1ms doing about 3.000.000 of init (It is a rough calculation :) ). |
OK let's merge that then. Last suggestion, change that: if (static::isValidKey($name)) {
$array = static::toArray();
return new static($array[$name]);
} into that: $array = static::toArray();
if (isset($array[$name])) {
return new static($array[$name]);
} ? |
In this case i think that duplicate the logic used to check if a key is valid isn't a problem. |
👍 |
Done. I'm already using the new feature (my repo until the pull request isn't merged) in this project https://github.com/valueobjects/geography/tree/master/src/Country where i have a DataProvider used by multiple Enum implementation (Iso31661Alpha2Code, Iso31661Alpha3Code, PhonePrefix). I provide a file based data provider (DefaultDataProvider) but applications that already have a data set in some places (DB, API, ...) can do a custom implementation of DataProviderInterface and use their data source. PS. The project is still a working in progress, so... no test, no docs and a little bit of mess all around :) |
Change calls to method toArray() from self::toArray() to static::toArray()
Thanks! I tagged 1.4.1 |
See #20
The idea is to allow subclasses to provide a data set different from constants list. The default implementation still use constants but you can extend it to use different data source; so for instance if you have data stored in a DB you can still use your specific Enum in type hinting, but fetch data from DB.