Download the PHP package nelexa/enum without Composer
On this page you can find all versions of the php package nelexa/enum. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package enum
Short Description PHP Enum implementation
License MIT
Homepage https://github.com/Ne-Lexa/enum
Informations about the package enum
nelexa/enum - Enum implementation for PHP
Table of Contents
- nelexa/enum - Enum implementation for PHP
- Table of Contents
- Installation
- Enum declaration
- Usage
- To loop a enum object
- To compare the enum values, use === operator
- Convert a string to enum object
- Convert a value to enum object
- Switch case
- Use enum in the type hint
- Add some logic to enum
- Initialization of values without constructor
- Class Synopsis
- Usage tips
- Generate PHPDoc for enum class
- Changelog
- License
Installation
Enum declaration
Usage
To loop a enum object
Output:
To compare the enum values, use ===
operator
Convert a string to enum object
Convert a value to enum object
Switch case
Output:
Use enum in the type hint
Example
Output:
Add some logic to enum
Example
Initialization of values without constructor
For example consider the planets of the solar system. Each planet knows its mass and radius, and can calculate its surface gravity and the weight of an object on the planet.
Here is how it looks:
The enum Planet class contains the initValue($value)
method, and each enum constant is declared with a value that to be passed to this method when it is created.
Here is a sample program that takes your weight on earth (in any unit) and calculates and prints your weight on all of the planets (in the same unit):
Output:
Class Synopsis
Usage tips
- Even though it is not mandatory to declare enum constants with UPPERCASE letters, it is in the best practice to do so.
- Enum classes can have fields and methods along with enum constants.
- Enum constructors are private. Only private constructors are allowed in enum classes. That’s why you can’t instantiate enum types using a new operator.
- Enum constants are created only once for the whole execution. All enum constants are created when you initially refer any enum constant in your code.
- Enum types can implement any number of interfaces.
- We can compare the enum constants using the
===
operator. - You can retrieve the enum constants of any enum type using the
values()
static method. Thevalues()
static method returns an array of enum constants. - The
ordinal()
static method is used to get the order of an enum constant in an enum type. - Enums are mostly used when you want to allow a limited set of options that remain constant for the whole execution and you know all possible options. For example, this could be choices on a menu or options of a combobox.
- Use private constants to exclude prompts from your IDE.
- Use PHPDoc to describe static enum initialization methods.
Generate PHPDoc for enum class
To use hints in the IDE, you need to declare special comments on the class with a list of static methods.
The \Nelexa\enum_docblock($enumOrEnumClass)
function should help with this.
Output:
Changelog
Changes are documented in the releases page.
License
The files in this archive are released under the MIT License
.
You can find a copy of this license in LICENSE
file.
All versions of enum with dependencies
ext-json Version *