Enums - An introduction

Enums - An introduction

C739f65cecbb38923d95e9b7cc0d2e63?s=128

Tommy Mühle

August 24, 2017
Tweet

Transcript

  1. Tommy Mühle | tommy-muehle.io Enums An introduction 1

  2. Tommy Mühle | tommy-muehle.io Tommy Mühle
 Software Engineer and Author

    2
  3. Tommy Mühle | tommy-muehle.io What is an enum? 3

  4. Tommy Mühle | tommy-muehle.io Tommy Mühle | tommy-muehle.io … a

    data type consisting of a set of named values called elements … of the type. 
 The enumerator names are usually identifiers that behave as constants in the language. https:/ /en.wikipedia.org/wiki/Enumerated_type
  5. Tommy Mühle | tommy-muehle.io How they are implemented in PHP?

    5
  6. Tommy Mühle | tommy-muehle.io There is no native implementation 6

  7. Tommy Mühle | tommy-muehle.io 7 https:/ /wiki.php.net/rfc/enum

  8. Tommy Mühle | tommy-muehle.io 8 https:/ /docs.hhvm.com/hack/enums/introduction

  9. Tommy Mühle | tommy-muehle.io Without enum classes 9

  10. Tommy Mühle | tommy-muehle.io 10 Avoid global constants // app.php

    const STATE_ACTIVE = 'active'; const STATE_COMPLETED = 'completed'; $app = new Application(); $app->setState(STATE_ACTIVE); // ... return $app->run();
  11. Tommy Mühle | tommy-muehle.io 11 class StateHandler { const STATE_ACTIVE

    = 'active'; const STATE_CANCELED = 'canceled'; public function handle($state) { if ($state !== self::STATE_ACTIVE) { // ... } // ... } } Avoid class constants
  12. Tommy Mühle | tommy-muehle.io With enum classes 12

  13. Tommy Mühle | tommy-muehle.io 13 final class Color { const

    __DEFAULT = 'black'; const RED = 'red'; private $value; public function __construct(string $color = null) { $this->value = $color ?? self::__DEFAULT; } public function __toString() { return $this->value; } }
  14. Tommy Mühle | tommy-muehle.io Advantages 14

  15. Tommy Mühle | tommy-muehle.io You can get a list of

    all the values 15
  16. Tommy Mühle | tommy-muehle.io 16 final class Color { //

    ... public function __construct(string $color = null) { $value = $color ?? self::__DEFAULT; if (!in_array($value, $this->all(), true)) { // ... } $this->value = $value; } public function all() : array { return (new \ReflectionClass(static::class))
 ->getConstants(); } // ... }
  17. Tommy Mühle | tommy-muehle.io You can type-hint 17

  18. Tommy Mühle | tommy-muehle.io 18 class Article { private $color;

    public function colorize(Color $color) { // ... } }
  19. Tommy Mühle | tommy-muehle.io You can enrich your classes with

    methods. 19
  20. Tommy Mühle | tommy-muehle.io 20 final class Color { //

    ... protected $value; // ... public function value() { return $this->value; } public function equals(Color $color) { // or any other business condition return $this->value === $color->value(); } }
  21. Tommy Mühle | tommy-muehle.io 21 final class Color { //

    ... protected $value; // ... public static function fromHex(string $hex) { // do something with $hex and get $color return new self($color); } public function format() { return 'My special color format'; } }
  22. Tommy Mühle | tommy-muehle.io You can extend this classes 22

  23. Tommy Mühle | tommy-muehle.io 23 // if class Color are

    not final final class SpecialColor extends Color { // add further values, enrich methods, etc. }
  24. Tommy Mühle | tommy-muehle.io Before you start 24

  25. Tommy Mühle | tommy-muehle.io 25 https:/ /packagist.org/search/?q=enum

  26. Tommy Mühle | tommy-muehle.io Summary 26

  27. Questions?

  28. Thank you! Slides http:/ /bit.ly/2isVOvl Images https:/ /pixabay.com/ @tommy_muehle