2017 © Pedro Peláez
 

library php-enum

Enumerated type for PHP

image

petrknap/php-enum

Enumerated type for PHP

  • Thursday, June 29, 2017
  • by petrknap
  • Repository
  • 1 Watchers
  • 0 Stars
  • 220 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 18 Versions
  • 24 % Grown

The README.md

Enumerated type for PHP

What is Enum?

In computer programming, an enumerated type (also called enumeration or enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but which are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily. -- Enumerated type - Wikipedia, The Free Encyclopedia, (*1)

Why use Enums instead of Constants?

Because it is safer and less scary than using constants. Don't trust me? Let see at this code:, (*2)

use PetrKnap\Enum\Readme\MyBoolean;

$isTrue = function (int $myBoolean)
{
    switch($myBoolean) {
        case MyBoolean::MY_TRUE:
            return true;
        case MyBoolean::MY_FALSE:
            return false;
    }
};

var_dump($isTrue(MyBoolean::MY_TRUE));  // true - correct
var_dump($isTrue(MyBoolean::MY_FALSE)); // false - correct
var_dump($isTrue(0));                   // none
var_dump($isTrue(1));                   // true - expected
var_dump($isTrue(2));                   // false
var_dump($isTrue((int) true));          // true - expected
var_dump($isTrue((int) false));         // none

And now the same code with enum instead of constants:, (*3)

use PetrKnap\Enum\Readme\MyBoolean;

$isTrue = function (MyBoolean $myBoolean): bool
{
    switch($myBoolean) {
        case MyBoolean::MyTrue:
            return true;
        case MyBoolean::MyFalse:
            return false;
    }
};

var_dump($isTrue(MyBoolean::MyTrue));  // true - correct
var_dump($isTrue(MyBoolean::MyFalse)); // false - correct

Run composer require petrknap/enum to install it. You can support this project via donation. The project is licensed under the terms of the LGPL-3.0-or-later., (*4)

The Versions

29/06 2017

dev-master

9999999-dev https://petrknap.github.io/docs/php-enum.html

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

19/06 2017

v2.1.0

2.1.0.0 https://petrknap.github.io/docs/php-enum.html

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

20/12 2016

dev-backup/2017-03-12_master

dev-backup/2017-03-12_master https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

20/12 2016

v2.0.5

2.0.5.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

09/10 2016

v2.0.4

2.0.4.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

09/10 2016

v2.0.3

2.0.3.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

09/10 2016

v2.0.2

2.0.2.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

02/09 2016

v2.0.1

2.0.1.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

10/08 2016

v2.0.0

2.0.0.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

10/08 2016

v1.1.1

1.1.1.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

08/08 2016

v1.1

1.1.0.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

18/07 2016

v1.0.2

1.0.2.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

15/04 2016

v1.0.1

1.0.1.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

24/01 2016

v1.0

1.0.0.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

24/01 2016

v0.4

0.4.0.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

24/01 2016

v0.3

0.3.0.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

24/01 2016

v0.2

0.2.0.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

23/01 2016

v0.1

0.1.0.0 https://github.com/petrknap/php-enum

Enumerated type for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires