PHP code example of neutronstars / enum

1. Go to this page and download the library: Download neutronstars/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/ */

    

neutronstars / enum example snippets


/**
 * @method static self ONE()
 * @method static self TWO()
 * @method static self THREE()
 */
class MyEnum extends \NeutronStars\Enum\Enum
{
   public const ONE = null;
   public const TWO = null;
   public const THREE = null;
}

/**
 * @method static self ONE()
 * @method static self TWO()
 * @method static self THREE()
 */
class MyStringEnum extends \NeutronStars\Enum\Enum
{
   public const ONE   = 'One';
   public const TWO   = 'Two';
   public const THREE = 'Three';
}

/**
 * @method static self ONE()
 * @method static self TWO()
 * @method static self THREE()
 */
class MyIntEnum extends \NeutronStars\Enum\Enum
{
   public const ONE   = 1;
   public const TWO   = 2;
   public const THREE = 3;
}

$two = MyStringEnum::TWO();

echo $two; // return TWO
echo $two->key; // return TWO
echo $two->value; // return Two

$three = MyStringEnum::from('THREE');

echo $three; // THREE
echo $three->key; // return THREE
echo $three->value; // return Three

$value = MyStringEnum::tryFrom($_GET['number']);
if ($value === MyStringEnum::ONE()) {
  echo 'The number is ONE !';
}

$cases = MyIntEnum::cases();

foreach (MyIntEnum::cases() as $case) {
    echo '- ' . $case->value;
}

$serialized = serialize(MyEnum::THREE());

$deserialized = MyEnum::from(unserialize($deserialized));
// $deserialized = MyEnum::THREE()

composer 

composer 

composer