Download the PHP package rikudou/enums-trait without Composer

On this page you can find all versions of the php package rikudou/enums-trait. 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 enums-trait

PHP Enums

This is yet another PHP enum implementation.

What is so different about this one? It uses trait which solves one problem all the other implementations I've seen have - extending some base enum class.

All the enums return the same instance for the same enum, so you can check for equality (===) and the result is true.

The constructor is made private, so you cannot directly construct an instance via new keyword (but you can create an constructor in your class and make it public if you need to, but I advise against it).

Every internal method is made private so you cannot extend the enum (well, you can but it would be useless).

Usage:

Creating enum

That's it, you just created an enum. It's a pretty basic one, that supports pretty much any value via magic method __callStatic. For example:

If you want IDE completion you have two options:

  1. create the static methods yourself
  2. add phpdoc comments to the class (for example PHPStorm supports it)

Create the methods yourself

Your IDE will now autocomplete for EnumValue1 and EnumValue2 static methods.

Add phpdoc comments

The result for IDE is the same as creating the methods yourself.

Limiting the allowed enums

If you don't like the idea that you can create pretty much any enum via the magic method, you can limit them like this:

Now your enum contains only Value1, Value2 and Value3. Anything else will throw InvalidArgumentException.

As you can see, the allowedValues() array doesn't care about the methods you create manually.

Using the enums

As you can see, you can typehint the enum and php itself will check that it's really an instance of the enum.

Then you can just check them in switch or if/else or whatever.

Getting enum value

If you need the value that the enum holds, you can use the getValue() method.

The methods constructed __callStatic magic method hold the value of the method name:

The methods you create have a value that you give them:

Some caveats

And that's all, folks. Now all that remains is waiting for a real enum implementation on the php side.


All versions of enums-trait with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4
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 rikudou/enums-trait contains the following files

Loading the files please wait ....