2017 © Pedro Peláez
 

library phpenum

A simple PHP Enum implementation, as an alternative to SplEnum

image

codinc/phpenum

A simple PHP Enum implementation, as an alternative to SplEnum

  • Thursday, May 17, 2018
  • by Codinc
  • Repository
  • 0 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

phpEnum

A simple PHP Enum implementation, as an alternative to SplEnum, allowing typesafety for values belonging to an enum., (*1)

Installation

composer require codinc/phpenum, (*2)

Usage

The abstract class supports extension by concrete classes that define constants as valid values., (*3)

```$xslt use Codinc\Type\Enum;, (*4)

class MyEnum extends Enum { const PERSONAL = 'personal'; const TEAM = 'team'; }, (*5)


Of course, the biggest advantage of an enum is to allow typehinting and built-in safety on methods on your set of values. ```$xslt public function doAnAction(MyEnum $value) { // Can only contain PERSONAL or TEAM }

The biggest advantage and difference of this implementation is that it is allowing native strict comparison. The Enum keeps track of the instantiated objects to ensure only one instance can be used., (*6)

```$xslt MyEnum::PERSONAL() === MyEnum::PERSONAL(); // === true, (*7)


It is supported to call the constant as a method without added overhead. In case you do desire the benefits of autocompletion, or desire to protect your constants, you can still define the public static methods yourself and call load on the Enum. ```$xslt use Codinc\Type\Enum; class MyEnum extends Enum { const PERSONAL = 'personal'; const TEAM = 'team'; const WORLD = 'world'; private const COMPANY = 'company'; public static function WORLD() { return self::load(self::WORLD); } public static function COMPANY() { return self::load(self::COMPANY); } }

```$xslt $personal = MyEnum::PERSONAL(); $world = MyEnum::WORLD();, (*8)


Unsupported values will throw an \InvalidArgumentException. ```$xslt MyEnum::load('personal'); // === MyEnum::PERSONAL() MyEnum::load('made_up'); // throws \InvalidArgumentException MyEnum::load($repository->fetchColumn($column));

The Versions

17/05 2018

dev-master

9999999-dev http://www.codinc.be

A simple PHP Enum implementation, as an alternative to SplEnum

  Sources   Download

Apache-2.0

The Requires

  • php >=7.1.0

 

The Development Requires

by Jan Deschuttere

php enum strict typesafety

17/05 2018

1.0.3

1.0.3.0 http://www.codinc.be

A simple PHP Enum implementation, as an alternative to SplEnum

  Sources   Download

Apache-2.0

The Requires

  • php >=7.1.0

 

The Development Requires

by Jan Deschuttere

php enum strict typesafety

16/05 2018

1.0.2

1.0.2.0 http://www.codinc.be

A simple PHP Enum implementation, as an alternative to SplEnum

  Sources   Download

Apache-2.0

The Requires

  • php >=7.1.0

 

The Development Requires

by Jan Deschuttere

php enum

16/05 2018

1.0.1

1.0.1.0 http://www.codinc.be

A simple PHP Enum implementation, as an alternative to SplEnum

  Sources   Download

Apache-2.0

The Requires

  • php >=7.1.0

 

The Development Requires

by Jan Deschuttere

php enum

16/05 2018

1.0

1.0.0.0 http://www.codinc.be

A simple PHP Enum implementation, as an alternative to SplEnum

  Sources   Download

Apache-2.0

The Requires

  • php >=7.1.0

 

The Development Requires

by Jan Deschuttere

php enum