PHP code example of tleckie / enum

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

    

tleckie / enum example snippets




/**
 * Class Vehicle
 *
 * @method static Vehicle CAR()
 * @method static Vehicle MOTORCYCLE()
 * @method static Vehicle BIKE()
 * @method static Vehicle TRICYCLE()
 * 
 * @author Teodoro Leckie Westberg <[email protected]>
 */
class Vehicle extends Enum 
{
    public const CAR = 1;
    public const MOTORCYCLE = 2;
    public const BIKE = 3;
    public const TRICYCLE = 4;
}

$vehicle = new Vehicle(3);

// Dynamic static methods available
$vehicle::CAR();           // new Vehicle(1)
$vehicle::MOTORCYCLE();    // new Vehicle(2)
$vehicle::BIKE();          // new Vehicle(3)
$vehicle::TRICYCLE();      // new Vehicle(4)
.
.
.
Vehicle::CAR();           // new Vehicle(1)
Vehicle::MOTORCYCLE();    // new Vehicle(2)
Vehicle::BIKE();          // new Vehicle(3)
Vehicle::TRICYCLE();      // new Vehicle(4)


public function edit(Vehicle $vehicle){
    //...
}

$object->edit(Vehicle::CAR());


$vehicle = new Vehicle(3);

$vehicle->getValue();  // int(3)
$vehicle->getKey();    // "BIKE"

$vehicle = new Vehicle(3);
$vehicle->getValues(); // [1,2,3,4]

$vehicle = new Vehicle(3);
$vehicle->getKeys();    //["CAR","MOTORCYCLE","BIKE","TRICYCLE"]

$vehicle = new Vehicle(3);
$vehicle->toArray();   //["CAR" => 1,"MOTORCYCLE" => 2,"BIKE" => 3,"TRICYCLE" => 4]

(string) Vehicle::MOTORCYCLE();             // "2"
(string) new Vehicle(3);                    // "3"
(string) new Vehicle( Vehicle::TRICYCLE() );// "4"
(string) new Vehicle( new Vehicle(1) );     // "1"