PHP code example of palabs / php-enum

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

    

palabs / php-enum example snippets


use PaLabs\Enum\Enum;

class Action extends Enum
{
    public static Action $VIEW, $EDIT;
}
Action::init();

function someAction(Action $action) {
    switch($action) {
        case Action::$VIEW:
            // some code
        break;
        case Action::$EDIT:
            // another code
        break;
        default:
            // ...
        break;
    }
}

$viewAction = Action::$VIEW;
if($viewAction->equals(ACTION::$EDIT)) {
 // ...
}

$allActions = Action::values();

use PaLabs\Enum\Enum;

class Planet extends Enum
{
    public static Planet 
        $MERCURY,
        $VENUS,
        $EARTH,
        $MARS,
        $JUPITER,
        $SATURN,
        $URANUS,
        $NEPTUNE;

    private float $mass;   // in kilograms
    private float $radius; // in meters
    
    public function __construct(float $mass, float $radius) {
        $this->mass = $mass;
        $this->radius = $radius;
    }

    private const G = 6.67300E-11;

    public function surfaceGravity(): float {
        return self::G * $this->mass / ($this->radius * $this->radius);
    }
    public function surfaceWeight(float $otherMass): float {
        return $otherMass * $this->surfaceGravity();
    }

}

Planet::$MERCURY = new Planet(3.303e+23, 2.4397e6);
Planet::$VENUS = new Planet(4.869e+24, 6.0518e6);
Planet::$EARTH = new Planet(5.976e+24, 6.37814e6);
Planet::$MARS = new Planet(6.421e+23, 3.3972e6);
Planet::$JUPITER = new Planet(1.9e+27,   7.1492e7);
Planet::$SATURN = new Planet(5.688e+26, 6.0268e7);
Planet::$URANUS = new Planet(8.686e+25, 2.5559e7);
Planet::$NEPTUNE = new Planet(1.024e+26, 2.4746e7);
Planet::init();

$yourEarthWeight = 65.0;
$mass = $yourEarthWeight / Planet::$EARTH->surfaceGravity();
foreach (Planet::values() as $planet) {
    sprintf("Your weight on %s is %f%n", $planet->name(), $planet->surfaceWeight($mass));
}

composer