Comments (9)
I agree that PHP needs an enumeration type. However, I feel like if that comes from anywhere besides the PHP core that it's a kludge and hack.
That being said, I'll at least consider this idea and play around with it a bit.
from ardent.
I totally agree with you. But as long as PHP doesn't have that implemented in core it's better to have it within a more used and general library.
from ardent.
You have been playing with this for 2 months ... any updated :)
from ardent.
I did do a bit of playing around with it. In short, I don't feel like any of the options really help. PHP needs enums in the core and I don't think trying to emulate them will be helpful.
I'm still open to suggestions though.
from ardent.
Enumerations requirements:
- Must be able to be serialized consistently
- Must be able to be used in type-hints
- Must be able to be auto-loaded
Considerations:
- Can it be used in
instanceof
? - Support casts to number?
from ardent.
Oops, wrong button. Meant comment, not close.
from ardent.
Must be able to be serialized consistently
Should be possible because constants accepting scalar values only - also it is possible to add the serializable interface
Must be able to be used in type-hints
It's implemented as a class which needs to be instantiated - so the name of the class is the type-hint
Must be able to be auto-loaded
Same as above - it's a class name classes will be autoloadable
Can it be used in instanceof?
Same as above - it's an instance of a class and $enumObject instanceof EnumClass
works fine
Support casts to number?
This isn't possible as is
Only with a workaround it could be done:
- Only allow numbers as values - throw an exception if this isn't the case
- cast the object to a string returning the current number as a string and than case the string into a number
$value = (int) (string) $enumObject;
- But this isn't nice
The above liked implementation allows all scalar types and returns the current const name on casting to a string.
Casting to the current value needs to be done using the __invoke
magic method = calling the enum object like a function.
from ardent.
After more consideration I still think this is really something that needs to be in PHP core and that a user-land implementation isn't useful enough.
from ardent.
I agree ... Enums are only useful as part of the language. In any case, you can "fake" enums perfectly well right now with class constants.
from ardent.
Related Issues (20)
- Filter Counter HOT 5
- No last/first methods on sets HOT 6
- Rename methods in Collections API. HOT 2
- SlicingIterator preserves keys: should it? HOT 5
- Define some basic naming conventions. HOT 17
- Run tests on travis HOT 4
- Binding to specific class (aka type template) HOT 2
- HashMap toArray() does not use $preserveKeys by default HOT 7
- Is having main collection interfaces always extending IteratorAggregate the best option? HOT 5
- Should Ardent have a TypeException? HOT 2
- Method visibility HOT 4
- Let Composer Handle Autoloading HOT 9
- Filter and Sort arbitrary Collections HOT 5
- move count upwards to Collection interface HOT 1
- Define Hashable Interface HOT 3
- how to use and test? HOT 2
- It's that better instead of using Arrays?
- Set should implement union, intersection, difference and symmetric difference. HOT 10
- Excessive star accrual HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ardent.