Skip to content

Latest commit

 

History

History
251 lines (205 loc) · 5.86 KB

enums.md

File metadata and controls

251 lines (205 loc) · 5.86 KB

➲ Enums:

Enums are also referred as enumerations. Which is a data type that consists of a set of named constants. This is used to define a fixed set of possible values to a variable.

☴ Overview:

  1. Need of Enum
  2. Enum Implementation
  3. Backed Enums
  4. Enum Methods
  5. Enum Static Methods
  6. Enum Constants
  7. Enum With Traits
  8. Enum Serialization
  9. Enum Usages
  10. Key Points And Limitations

✦ Need of Enum:

  • Type Safety: It provides type safety for a variable by holding defined values.
  • Readability: It make code more readable by using descriptive names for constants.
  • Reduced Errors: It can help to prevent invalid values.

✦ Enum Implementation:

Syntax:

enum ConstantName {
    case ValueName1;
    case ValueName2;
    case ValueName3;
}

$var = ConstantName::ValueName1;

✦ Backed Enums:

Syntax:

enum ConstantName: string {
    case ValueName1='value1';
    case ValueName2='value2';
    case ValueName3='value3';
}

$var = ConstantName::ValueName1->value;

✦ Enum Methods:

It can hold methods like classes.

interface HTTPStatusInterface {
    public function getStatusCode();
}

enum HTTPStatus implements HTTPStatusInterface {
    case OK;
    case NotFound;
    case InternalServerError;

    public function getStatusCode(): int
    {
        return match($this) {
            HTTPStatus::OK => 200,
            HTTPStatus::NotFound => 404,
            HTTPStatus::InternalServerError => 500
        };
    }
}

echo HTTPStatus::OK->getStatusCode();

✦ Enum Static Methods:

It can holds static methods like classes.

interface HTTPStatusInterface {
    public static function getStatusCode($status);
}

enum HTTPStatus implements HTTPStatusInterface {
    case OK;
    case NotFound;
    case InternalServerError;

    public static function getStatusCode($status): static
    {
        return match(true) {
            $status == 200 => static::OK,
            $status == 404 => static::NotFound,
            $status == 500 => static::InternalServerError
        };
    }
}

echo HTTPStatus::getStatusCode(200)->name;

✦ Enum Constants:

Enum can hold constants with an access modifiers such as a private, protected and public.

enum Product {
    case Shirt;
    case Pant;

    public const MediumShirt = Size::Medium;
    public const LargePant = Size::Large;
}

enum Size {
    case Small;
    case Medium;
    case Large;

    public const SmallSize = 36;
    public const MediumSize = 40;
    public const LargeSize = 46;
}

print Product::Pant::MediumShirt->name . PHP_EOL;

print Product::Pant::MediumShirt::MediumSize;

✦ Enum With Traits:

Enum can use trait same as class behavior.

trait PriceTrait {
    public function calculatePrice($size): int
    {
        return match(true) {
            ($this == static::Shirt && $size == Size::Small) => 100, 
            ($this == static::Shirt && $size == Size::Medium) => 120, 
            ($this == static::Shirt && $size == Size::Large) => 140,
            ($this == static::Pant && $size == Size::Small) => 150, 
            ($this == static::Pant && $size == Size::Medium) => 170, 
            ($this == static::Pant && $size == Size::Large) => 190, 
        };
    }
} 

enum Product {
    use PriceTrait;
    case Shirt;
    case Pant;
}

enum Size {
    case Small;
    case Medium;
    case Large;
}

print Product::Shirt->calculatePrice(Size::Small);

✦ Enum Serialization:

Enum can be serialized into string and unserialized back into original form.

enum SizeOptions: int {
    case Small = 36;
    case Medium = 40;
    case Large = 46;
}

print serialize(SizeOptions::cases());
print_r(unserialize(serialize(SizeOptions::cases())));

✦ Enum Usages:

  • Enums can be used as type hints for function parameters and their return types.
  • Enums can be used in switch statements for concise and type-safe comparisons.
  • Enums can be compared using comparison operators.
  • Enums can iterate over enum cases using loops such as foreach and while.

Example:

enum HTTPStatus: int {
    case OK = 200;
    case NotFound = 404;
    case InternalServerError = 500;
}

$statusCode = HTTPStatus::NotFound;
echo $statusCode->value; // Output: 404

Example:

enum HTTPStatus {
    case OK;
    case NotFound;
    case InternalServerError;
}

function getStatusCode(HTTPStatus $status): int {
    return match ($status) {
        HTTPStatus::OK => 200,
        HTTPStatus::NotFound => 404,
        HTTPStatus::InternalServerError => 500
    };
}

echo getStatusCode(HTTPStatus::OK);

Example:

enum SortOrder {
	case Asc;
	case Desc;
}

DB::select("name")->fetchAll(SortOrder::Desc);

Example:

enum SizeOptions: int {
    case Small = 36;
    case Medium = 40;
    case Large = 46;
}
print "<label>Size</label> <br>";
print "<select name=\"size\">";
foreach (SizeOptions::cases() as $case) {
    print "<option value=\"" . $case->value . "\">" . $case->name . "</option>";
}
print "</select>";

✦ Key Points And Limitations:

Key Points:

  • It is a case-insensitive.
  • Enum cases can associate values.
  • Enums can be backed by values such as integers or strings.
  • Enums can be defined and marked as unit-safe to prevent unintentional type conversions.

Limitations:

  • Enum can support in PHP from 8.1 and later versions.
  • Enum behave like classes, But it cannot be instantiated with new keyword like classes.
  • It cannot be extended with extend keyword like classes.

⇪ To Top

❮ Previous TopicNext Topic ❯

⌂ Goto Home Page