PHP code example of rikudou / enums-trait

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

    

rikudou / enums-trait example snippets




MyCoolEnum::Test() == MyCoolEnum::Test(); // true
MyCoolEnum::Test() === MyCoolEnum::Test(); // true
MyCoolEnum::Test() == MyCoolEnum::Test2(); // false
MyCoolEnum::Test() === MyCoolEnum::Test2(); // false



use rikudou\PHPEnum\EnumTrait;

class MyCoolEnum
{
  use EnumTrait;
}




$testValue = MyCoolEnum::TestValue();
$testValue2 = MyCoolEnum::TestValue2();
$testValue3 = MyCoolEnum::PrettyMuchAnythingCanGoHere();



use rikudou\PHPEnum\EnumTrait;

class MyCoolEnum
{
  use EnumTrait;
  
  public static function EnumValue1() {
    return static::_get("EnumValue1");
  }
  
  public static function EnumValue2() {
    return static::_get("EnumValue2");
  }
}



use rikudou\PHPEnum\EnumTrait;

/**
 * @method static static EnumValue1()
 * @method static static EnumValue2()
 */
class MyCoolEnum
{ 
  use EnumTrait;
}



use rikudou\PHPEnum\EnumTrait;

class MyCoolEnum
{
  use EnumTrait;
  
  private static function allowedValues()
  {
    return [
      "Value1",
      "Value2",
    ];
  }
  
  public static function Value3()
  {
    return static::_get("Value3");
  } 
}



function doSomething(MyCoolEnum $myCoolEnum)
{
  switch ($myCoolEnum) {
    case MyCoolEnum::Value1():
      return "Value1";
    case MyCoolEnum::Value2():
      return "Value2";
    // etc
  }
}



echo MyCoolEnum::SomeCoolValue()->getValue(); // echoes "SomeCoolValue"



use rikudou\PHPEnum\EnumTrait;

class MyCoolEnum {
  
  use EnumTrait;
  
  public static function MyCoolValue() {
    return static::_get(1);
  }
  
}

echo MyCoolEnum::MyCoolValue()->getValue(); // echoes 1



use rikudou\PHPEnum\EnumTrait;

class MyCoolEnum {
  
  use EnumTrait;
  
  public static function Value1() {
    return static::_get("Value");
  }
  
  public static function Value2() {
    return static::_get("Value"); // same as in Value1()
  }
  
}

var_dump(MyCoolEnum::Value1() === MyCoolEnum::Value2()); // dumps true