Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Enums - An introduction

Enums - An introduction

Tommy Mühle

August 24, 2017
Tweet

More Decks by Tommy Mühle

Other Decks in Programming

Transcript

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

    View Slide

  2. Tommy Mühle | tommy-muehle.io
    Tommy Mühle

    Software Engineer and Author
    2

    View Slide

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

    View Slide

  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

    View Slide

  5. Tommy Mühle | tommy-muehle.io
    How they are
    implemented in PHP?
    5

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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();

    View Slide

  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

    View Slide

  12. Tommy Mühle | tommy-muehle.io
    With enum
    classes
    12

    View Slide

  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;
    }
    }

    View Slide

  14. Tommy Mühle | tommy-muehle.io
    Advantages
    14

    View Slide

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

    View Slide

  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();
    }
    // ...
    }

    View Slide

  17. Tommy Mühle | tommy-muehle.io
    You can
    type-hint
    17

    View Slide

  18. Tommy Mühle | tommy-muehle.io 18
    class Article
    {
    private $color;
    public function colorize(Color $color)
    {
    // ...
    }
    }

    View Slide

  19. Tommy Mühle | tommy-muehle.io
    You can enrich your
    classes with methods.
    19

    View Slide

  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();
    }
    }

    View Slide

  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';
    }
    }

    View Slide

  22. Tommy Mühle | tommy-muehle.io
    You can extend
    this classes
    22

    View Slide

  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.
    }

    View Slide

  24. Tommy Mühle | tommy-muehle.io
    Before
    you start
    24

    View Slide

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

    View Slide

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

    View Slide

  27. Questions?

    View Slide

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

    View Slide