PHP code example of linmad / ordinary-enum

1. Go to this page and download the library: Download linmad/ordinary-enum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

linmad / ordinary-enum example snippets


class StoreEnum extends Enum
{
  public const __default = self::GROCERY;
  public const GROCERY   = Grocery::class;
  public const COSMETIC  = Cosmetic::class;
}

...
// Let's imagine you have fabric with stores
$fruitStore = $this->storeFabric()->create(StoreEnum::GROCERY);
...
// Now let's see how can be used Enum in create method
...
public function create(Enum $storeType): StoreInterface
{
  try {
    $storeType = new StoreEnum($type);
  } catch(InvalidEnumTypeException $e) {
    throw new \RuntimeException('Unable to create store from factory');
  }

   return new $storeType->getValue();
}
...