PHP code example of bonndan / phpenums

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

    

bonndan / phpenums example snippets



class Fruit extends \Pava\Enum
{
    /**
     * @var Fruit
     */
    public static $APPLE;

    /**
     * @var Fruit
     */
    public static $BANANA;
}



$mix = function (Fruit $a, Fruit $b) {};

$blend = $mix(Fruit::$APPLE, Fruit::$BANANA);

$grow = function () : Fruit { return Fruit::$APPLE };

assert(Fruit::$APPLE instanceof Fruit);



//class Fruit ...

Pava\register(Fruit::class);


//not possible
$a = function (Fruit $a = Fruit::$BANANA) {};



class Fruit extends \Pava\Enum
{
    private $color;

    /**
     * @var Fruit
     */
    public static $APPLE;

    /**
     * @var Fruit
     */
    public static $BANANA;

    public function color() : string
    {
        return $this->color;
    }

    public function __invoke()
    {
        if ($this->name() == 'BANANA')
            $this->color = 'yellow';
        if ($this->name() == 'APPLE')
            $this->color = 'green';
    }
}
Pava\register(Fruit::class);

echo 'The apple is ' . Fruit::$APPLE->color();