Download the PHP package dbalabka/php-enumeration without Composer

On this page you can find all versions of the php package dbalabka/php-enumeration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-enumeration

PHP Enumeration classes

Build Status Coverage Status

Implementation of Enumeration Classes in PHP. The better alternative for Enums.

In contrast to Magic methods and Reflection to provide better performance and code autocompletion. The Enumeration class holds a reference to a single Enum element represented as an object (singleton) to provide the possiblity to use strict (===) comparision between the values. Also, it uses static properties that can utilize the power of Typed Properties. The Enumeration Classes are much closer to other language implementations like Java Enums and Python Enums.

Declaration

A basic way to declare a named Enumeration class:

Note! You should always call the Enumeration::initialize() method right after Enumeration Class declaration. To avoid manual initialization you can setup the StaticConstructorLoader provided in this library.

Declaration with Typed Properties support:

By default an enumeration class does not require the value to be provided. You can use the constructor to set any types of values.

  1. Flag enum implementation example:

  2. You should override initializeValues() method to set custom values for each Enum element.

Declaration rules that the developer should follow:

  1. The resulting Enumeration class should be marked as final. Abstract classes should be used to share functionality between multiple Enumeration classes.

    ...Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances. On the other hand, it makes sense to allow sharing some common behavior between a group of enumerations... (from Python Enum documentation)

  2. Constructor should always be declared as non-public (private or protected) to avoid unwanted class instantiation.
  3. Implementation is based on assumption that all class static properties are elements of Enum.
  4. The method Dbalabka\Enumeration\Enumeration::initialize() should be called after each Enumeration class declaration. Please use the StaticConstructorLoader provided in this library to avoid boilerplate code.

Usage

Basic usage

Match expression

PHP 8 is going to support match expression which simplifies usage of enums:

More usage examples

Known issues

Readonly Properties

In the current implementation, static property value can be occasionally replaced. Readonly Properties is aimed to solve this issue. In an ideal world Enum values should be declared as a constants. Unfortunately, it is not possible in PHP right now.

Also, see most recent Write-Once Properties RFC that aimed to address this issue.

Class static initialization

This implementation relies on class static initialization which was proposed in Static Class Constructor. The RFC is still in Draft status but it describes possible workarounds. The simplest way is to call the initialization method right after the class declaration, but it requires the developer to keep this in mind. Thanks to Typed Properties we can control uninitialized properties - PHP will throw an error in case of access to an uninitialized property. It might be automated with custom autoloader Dbalabka\StaticConstructorLoader\StaticConstructorLoader provided in this library:

Also, it would be very helpful to have expression based properties initialization (see C# example):

See examples/class_static_construct.php for example.

Serialization

There is no possibility to serialize the singleton. As a result, we have to restrict direct Enum object serialization.

New custom object serialization mechanism does not help with singleton serialization but it gives the possibility to control this in the class which holds the references to Enum instances. Also, it can be worked around with Serializable Interface in a similar way. For example, Java handles Enums serialization differently than other classes, but you can serialize it directly thanks to readResolve(). In PHP, we can't serialize Enums directly, but we can handle Enums serialization in class that holds the reference. We can serialize the name of the Enum constant and use valueOf() method to obtain the Enum constant value during unserialization. So this problem somewhat resolved the cost of a worse developer experience. Hope it will be solved in future RFCs.

See complete example in examples/serialization_php74.php.

Callable static properties syntax

Unfortunately, it is not easy to use callable that is stored in static property. There was a syntax change since PHP 7.0 which complicates the way to call callable.

It is the main disatvatige of static class properties.

It can be workarounded by magic calls using __callStatic but such option suffers from missing autosuggestion, negative performance impact and missing static analysis.

It would be helpful to have PHP built-in suppoort for late (in runtime) constants initialization or/and class constants initialization using simple expressions:

Still, calling Enum::FOO() will try to find a method instead of trying to treat constant's value as a callable. We assume, that such PHP behavior can be improved.

Existing solutions

Libraries:

PHP native:

(there are a lot of other PHP implementations)

References


All versions of php-enumeration with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package dbalabka/php-enumeration contains the following files

Loading the files please wait ....