Download the PHP package dnl-blkv/simple-php-enum without Composer

On this page you can find all versions of the php package dnl-blkv/simple-php-enum. 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 simple-php-enum

Simple PHP Enum

Description

A simple C/C++ alike PHP library for Enums.

Stats and Health

Travis CI codecov Latest Stable Version License

PHP Version Support

The package is new and thus only supports PHP >=7.0.

How To Basic

Installation

  1. Install composer
  2. Open your project folder in terminal
  3. Enter composer require dnl-blkv/simple-php-enum
  4. Wait for the composer to finish the job
  5. Now, you can start using the Simple PHP Enums as described below!

Defining Enums

Defining a basic Enum with the package is straightforward:

Here null means auto-determined ordinal value, or auto-ordinal. The default auto-ordinal is 0. The further auto-ordinal values are determined as {previous ordinal} + 1.

Enum constant names MUST be PSR-1-compliant AND start from a capital letter. If a constant name does not conform with the rules, the constant is ignored.

Creating

Once the class is defined, the enums can be acquired as:

Or:

Or:

In the examples above, if the name or the ordinal is not defined, exceptions will be thrown (UndefinedEnumNameException and UndefinedEnumOrdinalException correspondingly).

Accessing

You can access the name (string representation) and the ordinal (numeric representation) of the enum:

Comparison

Equality

The enums can be checked for equality as such:

Intuitively, two enums of different types are never equal. If we have an enum of type SomeOtherEnum with const VALUE = 0; then the following holds:

Comparison by Ordinal

It is also possible to compare the Simple PHP Enums by their ordinal values. There are four methods defined for this, as shown below:

If two enums of different types are compared, the InvalidArgumentException is thrown.

How To Advanced

Defining Enums with Custom Ordinals

Besides letting the library assign the ordinals automatically, you could manually assign custom integer values to the ordinals:

In this case the enums will be defined as following:

Duplicate Ordinals

Similarly to the vanilla C/C++ enums, this Simple PHP Enums allow for duplicate ordinals. This may be used for tackling such cases as a default value:

For the enum defined above, the following will hold:

If you are getting an enum with duplicate ordinal using a magic method or by name, it works as usual.

However, if you get it by an ordinal, the behavior is slightly different, and you have two options as shown below:

More Equality

The Simple PHP Enum library only creates each enum object once and then reuses it. Therefore, the enums are comparable with === or its alias isSame. This kind comparison is stricter than isEqual. Whereas isEqual only accounts for the enum type and ordinal, isSame also takes the name into account:

Checking Existence of Names and Ordinals

If you wish to check whether or not certain enum type has a given name or ordinal, there are methods allowing you to easily do so:

Converting to String

The enums have an embedded magical mechanism for serialization:

Notes

Extension

All the internals of the AbstractEnum class are either public or protected. Therefore, it is completely open for extension and allows you to build your own, more complex constructions on top of it.

Use with Databases

If you opt to use these enums with databases and store the ordinals, I would recommend to make sure that no stored enum has duplicate ordinals. Otherwise, it could happen that you store DEFAULT = 0, but receive IPA = 0 upon recreation.


All versions of simple-php-enum with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0
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 dnl-blkv/simple-php-enum contains the following files

Loading the files please wait ....