Download the PHP package dreamscapes/enumeration without Composer
On this page you can find all versions of the php package dreamscapes/enumeration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dreamscapes/enumeration
More information about dreamscapes/enumeration
Files in dreamscapes/enumeration
Package enumeration
Short Description A simple Typedef Enum implementation for php
License BSD-3-Clause
Homepage https://github.com/Dreamscapes/Enumeration
Informations about the package enumeration
Enumerations for PHP
Every php programmer at some point wants to put some structure to all those constants defined in their code. Usually one would put those constants under a dedicated class, but that's it; no additional features, no real benefit, no nothing.
What if you could get more?
This small library aims at providing additional features to your "Enumerations" - a set of static values that represent something meaningful to your code.
The problem with constants
Let's say our program will work with animals - various different animals, and we would like each type of animal to have a distinct value ( which I completely made up in this example ). Here's what many programmers currently do:
While this certaily works, there is a better way of defining those.
Defining the constants as a class has several benefits:
- You can use real namespacing, which can save you a few typing when using the class
- It feels more natural to use
Animal::Horse
thanMYAPP_ANIMAL_HORSE
- Since it's a class it opens up new possibilities and ideas -> that's where this library comes to use :)
How this library helps
The above example with a class introduces some issues but also opens up new possibilities.
- There's nothing preventing the programmer from instantiating the class
- What if you wanted to do it the opposite way? -> you have a value and you want to know the constant's name that holds such value in the enumeration?
- What if you wanted to check if a constant is defined in the Enumeration?
- What if you wanted to type-hint an enumerated value in a function/method's parameter list?
Let's take a look at another example that demonstrates the use of Enumerations provided by this library.
As you can see, suddenly there's much more you can possibly do with a class as simple as enumeration can be. Learn more in the API Docs which also include code examples and full method descriptions.
Semantic methods
Enumeration contains several semantically named methods you can use to write code that can be understood simply by looking at the code.
Consider following example:
More methods are available - be sure to check out API Docs!
Installation
Via Composer:
composer require dreamscapes\enumeration:dev-master
Composer's autoloading is supported so as long as you require "vendor/autoload.php";
somewhere in your code you can simply start using it.
Documentation
API documentation is available online - it includes all public methods you can use and also several code samples and use cases.
Offline documentation
Sure! Just install the development dependencies and generate the docs.
Documentation is now available at ./docs/index.html.
License
This software is licensed under the BSD (3-Clause) License. See the LICENSE file for more information.