pamil/enum

Enum implementing the flyweight pattern.

v1.0.0 2017-12-19 11:40 UTC

This package is auto-updated.

Last update: 2024-10-29 02:12:31 UTC


README

Enum

This library extends myclabs/php-enum with ability to compare enums by their identity (if static methods are used to create them).

Usage

  1. Require this package:

    $ composer require pamil/enum
  2. Create your enum:

    <?php
    
    use Pamil\Enum\Enum;
    
    /**
     * @method static static active()
     * @method static static inactive()
     */
    final class SampleEnum extends Enum
    {
        protected const active = 1;
        protected const inactive = 2;
    }
  3. Compare your enums by identity:

    var_dump(SampleEnum::active() === SampleEnum::active()); // true

Caveats

  • It does not work when creating enums by the constructor:

    var_dump(new SampleEnum(1) === SampleEnum::active()); // false
  • It does not work when you deserialise serialised enums:

    var_dump(unserialize(serialize(SampleEnum::active())) === SampleEnum::active());