Download the PHP package divengine/enum without Composer
On this page you can find all versions of the php package divengine/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download divengine/enum
More information about divengine/enum
Files in divengine/enum
Package enum
Short Description Div PHP Enum Solution
License GPL-3.0-or-later
Homepage https://divengine.com
Informations about the package enum
Div PHP Enum Solution 1.0.2
This is a PHP Enum Solution using classes and type hinting. Also you can build a taxonomies of enums!
An enumeration type, "enum" for short, is a data type to categorise named values. Enums can be used instead of hard coded strings to represent, for example, the status of a blog post in a structured and typed way.
In July 2019, I wrote a gist searching a solution for this.
https://gist.github.com/rafageist/aef9825b7c935cdeb0c6187a2d363909/revisions
Then I convert the gist in a real project. https://www.phpclasses.org/package/11366-PHP-Implement-enumerated-values-using-classes.html
The problem
Before 8.1, PHP didn't have a native enum type, only a very basic SPL implementation (https://www.php.net/manual/en/class.splenum.php), but this really doesn't cut the mustard. Some solutions using constants, but not resolve the problem. How to validate HOT or COLD ?
From 8.1, PHP have a enums implementation (https://www.php.net/manual/en/language.types.enumerations.php).
And now you can combine this solution with the new features of PHP.
There's a popular package called myclabs/php-enum. It's really awesome, but have a problem because we lose static analysis benefits like auto completion and refactoring.
The solution
Use PHP!
The class divengine\enum help you, but remember: the most important solution is the concept of this library.
With this class, you can solves the following problems:
- Constants with different names and equal value can be used as function arguments
- Lose static analysis benefits like auto completion and refactoring
- Maintaining duplicated code when use docblock type hints to solve the second problem
We need have built-in enums in PHP ! But, for now, this is a solution.
Installation
Composer:
Manual:
Clone the repo:
Include the lib:
Example
First, define your enums. You can build a taxonomy !!!:
Enums.php
Second, use your enums:
Do you like Java programming language?
You actually can switch on enums, but you can't switch on Strings until Java 7. You might consider using polymorphic method dispatch with Java enums rather than an explicit switch. Note that enums are objects in Java, not just symbols for ints like they are in C/C++. You can have a method on an enum type, then instead of writing a switch, just call the method - one line of code: done!
One of the nice things about this approach is that it is simply impossible to get certain types of errors. You can't miss a switch case (you can incorrectly implement a method for a particular constant, but there's nothing that will ever totally prevent that from happening!). There's no switch "default" to worry about. Also, I've seen code that puts enum constants into arrays and then indexes into the arrays - this opens the possibility of array index out of bounds exceptions - just use the enum! Java enums are very, very powerful. Learn all that you can about them to use them effectively.
Also note if you have several enum constants that all have the same behavior for a particular method (like days of the week, in which weekend days have the same behavior and the weekdays Tuesday through Thursday also share the same behavior), you can simply gather that shared code in an enum method that is not overridden by every constant (final protected) and then call that method from the appropriate methods. So, add "final protected void commonMethod() { ... }" and then the implementation of method() in each constant would just call commonMethod().
And.... what about PHP ? This is a similar solution ...
Enjoy!
Documentation: https://divengine.org
--
@rafageist
Eng. Rafa Rodriguez