PHP code example of nilay-jp / php-enum

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

    

nilay-jp / php-enum example snippets




// "HIGH"
var_dump(Height::HIGH()->name()); 

// true
var_dump(Height::HIGH()->equals(Height::HIGH()));

// false
var_dump(Height::HIGH()->equals(Height::MEDIUM()));

// 0 | 1 | ... | n
var_dump(Height::HIGH()->ordinal());

// Height.MEDIUM
var_dump(Height::valueOf("MEDIUM"));

// Array<Height> [Height.HIGH, Height.MEDIUM, Height.LOW]
var_dump(Height::values());


namespace com.example.app;

use jp\nilay\enum\Enum;

class Height extends Enum
{
    #[Enum]
    public static function HIGH(): Height { return new static(); }

    #[Enum]
    public static function MEDIUM(): Height { return new static(); }

    #[Enum]
    public static function LOW(): Height { return new static(); }
}



// "High"
var_dump(Height::HIGH()->getName()); 

// "H"
var_dump(Height::HIGH()->getAbbr()); 


namespace com.example.app;

use jp\nilay\enum\Enum;

class Height extends Enum
{
    protected string $name;
    protected string $abbr;

    public function __construct(string $name, string $abbr)
    {
        parent::__construct();
        $this->name = $name;
        $this->abbr = $abbr;
    }

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

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

    #[Enum]
    public static function HIGH(): Height { return new static("High", "H"); }

    #[Enum]
    public static function MEDIUM(): Height { return new static("Medium", "M"); }

    #[Enum]
    public static function LOW(): Height { return new static("Low", "L"); }
}