PHP code example of epalshin / enum

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

    

epalshin / enum example snippets


use Palshin\Enum\Enum;

/**
 * @method static self CLIENT() = 1
 * @method static self ADMIN()
 * @method static self MANAGER()
*/
final class UserRole extends Enum
{
}

UserRole::MANAGER() instanceof UserRole; // true
UserRole::MANAGER() === UserRole::MANAGER(); // true
UserRole::MANAGER()->value; // 3
UserRole::all(); // [ UserRole::CLIENT(), UserRole::ADMIN(), UserRole::MANAGER() ]
UserRole::toArray(); // [ 'CLIENT' => 1, 'ADMIN' => 2, 'MANAGER' => 3 ]
UserRole::toValues(); // [1, 2, 3]
UserRole::toNames(); // ['CLIENT', 'ADMIN', 'MANAGER']
UserRole::CLIENT()->id(); // FQCN with enum member name: App\Enums\UserRole::CLIENT
echo UserRole::CLIENT(); // print "1"
echo json_encode(['enumMember' => UserRole::CLIENT()]); // print "{enumMember:1}"

use Palshin\Enum\Enum;

/**
 * @method static self CLIENT() = 1
 * @method static self ADMIN()
 * @method static self MANAGER()
*/
final class UserRole extends Enum
{
}

use Palshin\Enum\Enum;

final class UserRole extends Enum
{
  private const CLIENT = 1;
  private const ADMIN = 2;
  private const MANAGER = 3;
}

use Palshin\Enum\Enum;

final class UserRole extends Enum
{
  public static function values(): array
  {
    return [
      'CLIENT' => 1,
      'ADMIN' => 2,
      'MANAGER' => 3,
    ]; 
  }
}

use Palshin\Enum\Enum;

/**
 * @method static self CLIENT()
*/
final class UserRole extends Enum
{
  private const ADMIN = 2;

  public static function values(): array
  {
    return [
      'MANAGER' => 3,
    ]; 
  }
}

use Palshin\Enum\Enum;

/**
 * @method static self CLIENT()
 * @method static self ADMIN()
 * @method static self MANAGER()
*/
final class UserRole extends Enum
{
  const CLIENT = 1;
  const ADMIN = 2;
  const MANAGER = 3;
}

UserRole::CLIENT === 1; // true
UserRole::CLIENT() instanceof UserRole; // true

use Palshin\Enum\Enum;

/**
 * @method static self CLIENT()
 * @method static self MANAGER()
 * @method static self ADMIN()
*/
class UserRole extends Enum
{
  public static function labels(): array
  {
    return [
      'CLIENT' => 'Client account',
      'MANAGER' => 'Manager account',
      'ADMIN' => 'Administrator account'
    ];
  }

  public static function descriptions(): array
  {
    return [
      'CLIENT' => 'Online store buyer',
      'MANAGER' => 'The person who is responsible for interaction with the client',
      'ADMIN' => 'The person who controls the managers'
    ];
  }
}

UserRole::CLIENT()->label; // Client account
UserRole::MANAGER()->description; // The person who is responsible for interaction with the client