PHP code example of dbalabka / php-enumeration

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

    

dbalabka / php-enumeration example snippets



use Dbalabka\Enumeration\Enumeration;

final class Action extends Enumeration
{
    public static $view;
    public static $edit;
}
Action::initialize();


final class Day extends Enumeration
{
    public static Day $sunday;
    public static Day $monday;
    public static Day $tuesday;
    public static Day $wednesday;
    public static Day $thursday;
    public static Day $friday;
    public static Day $saturday; 
}
Day::initialize();

    
    final class Flag extends Enumeration
    {
        public static Flag $ok;
        public static Flag $notOk;
        public static Flag $unavailable;
    
        private int $flagValue;
    
        protected function __construct()
        {
            $this->flagValue = 1 << $this->ordinal();
        }
    
        public function getFlagValue() : int
        {
            return $this->flagValue;
        }
    }
    

    
    
    final class Planet extends Enumeration
    {
        public static Planet $mercury;
        public static Planet $venus;
        public static Planet $earth;
        public static Planet $mars;
        public static Planet $jupiter;
        public static Planet $saturn;
        public static Planet $uranus;
        public static Planet $neptune;
    
        private float $mass;   // in kilograms
        private float $radius; // in meters
    
        // universal gravitational constant  (m3 kg-1 s-2)
        private const G = 6.67300E-11;
    
        protected function __construct(float $mass, float $radius)
        {
            $this->mass = $mass;
            $this->radius = $radius;
        }
        
        protected static function initializeValues() : void
        {
            self::$mercury = new self(3.303e+23, 2.4397e6);
            self::$venus   = new self(4.869e+24, 6.0518e6);
            self::$earth   = new self(5.976e+24, 6.37814e6);
            self::$mars    = new self(6.421e+23, 3.3972e6);
            self::$jupiter = new self(1.9e+27,   7.1492e7);
            self::$saturn  = new self(5.688e+26, 6.0268e7);
            self::$uranus  = new self(8.686e+25, 2.5559e7);
            self::$neptune = new self(1.024e+26, 2.4746e7);
        }
    
        public function surfaceGravity() : float 
        {
            return self::G * $this->mass / ($this->radius * $this->radius);
        }
    
        public function surfaceWeight(float $otherMass) {
            return $otherMass * $this->surfaceGravity();
        }
    }
    


use Dbalabka\Enumeration\Examples\Enum\Action;

$viewAction = Action::$view;

// it is possible to compare Enum elements
assert($viewAction === Action::$view);

// you can get Enum element by name 
$editAction = Action::valueOf('edit');
assert($editAction === Action::$edit);

// iterate over all Enum elements
foreach (Action::values() as $name => $action) {
    assert($action instanceof Action);
    assert($name === (string) $action);
    assert($name === $action->name());
}


use Dbalabka\Enumeration\Examples\Enum\Action;

$action = Action::$view;

echo match ($action) {
   Action::$view => 'View action',
   Action::$edit => 'Edit action',
}


// It is possible but don't do it
Action::$view = Action::$edit;
// Following isn't possible in PHP 7.4 with declared properties types
Action::$view = null;

 
use Dbalabka\StaticConstructorLoader\StaticConstructorLoader;

$composer = 

class Enum {
    // this is not allowed
    public static $FOO = new Enum();
    public static $BAR = new Enum();
}


// The following line will throw an exception
serialize(Action::$view);

class SomeClass
{
    public Action $action;

    public function __serialize()
    {
        return ['action' => $this->action->name()];
    }

    public function __unserialize($payload)
    {
        $this->action = Action::valueOf($payload['action']);
    }
}

// Instead of using syntax
Option::$some('1'); // this line will rise an error "Function name must be a string"
// you should use 
(Option::$some)('1');

Option::some('1');

class Enum {
   // this is not allowed
   public const FOO = new Enum();
   public const BAR = new Enum();
}