Understanding the PHP Enumerable

Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature seems like you define a PHP class, but it seems to method.

    For an example

    <?php
    
      enum Cars {
        case Speed;
        case Model;
        case Distance;
      }
    
    ?>

    Definitely, in the previous example, I defined my new type as a “Cars”. But how to access it? Anyway, In the next section, I am going to illustrate all parts of this concept. Let’s dive right in.

    Get the Case Name Or Assign Value for the PHP Enumerable Type

    Firstly, to assign a value to the “enum” case or get the case name, we have to use the following code.

    Get the case name of the “enum”.

    <?php
    
      enum Cars {
        case Speed;
        case Model;
        case Distance;
        
        public function getCarSpeed() {
          return $this->Speed;
        }
    
      }
     
      function print_car_speed(Cars $val ) {
        echo "The Car " . $val->name;
      }
      
      print_car_speed(Cars::Speed);
      
    ?>

    The output would be like the below.

    The Car Speed

    In the previous example, we just got the name of the case that is already inside the enumeration. But how to assign a value to this case?

    To do that, you have to define the enumeration with a data type. So the “enum” would be like the following code if it is an integer enumeration.

    <?php
    
      enum Cars: int {
        case Speed = 500;
        case Model = 2010;
        case Distance = 100000;
      }
    
    ?>

    In the following part, we are going to cover the enumeration types.

    Actually, the enumeration has two types of cases, which are as the following.

    • Pure Enum: refers to all enumerations that have no scalar types.
    • Backed Enum: refers to the ll enumerations that have scalar types like the above one.

    So to get the values of the previous Backed Enum, you have to write the following code.

    <?php
    
      enum Cars: int {
        case Speed = 500;
        case Model = 2010;
        case Distance = 100000;
      
        public function getCarSpeed(): int {
          return $this->Speed;
        }
    
      }
      
      function printTheCarSpeed( Cars $car ) {
        echo "The Car " . $car->name . " is " . $car->value;
      }  
      
      $car = Cars::Speed;
      printTheCarSpeed($car);
    
    ?>

    So the result would be as below.

    The Car Speed is 500

    Note that, you will not able to get a reference for the enumerations.

    Wrapping UP

    PHP enumeration is defining a new custom data type, which means a special kind of PHP object. So it is similar to the PHP class and its cases are something like an instance from the class. So we can say an enumeration is a valid object.

    Previous Article

    PHP Ternary Operator: Shorten Your Code

    Next Article

    PHP NOT ( ! ) Operator: A Comprehensive Guide

    Write a Comment

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Subscribe to Get Updates

    Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
    No spam. Unsubscribe anytime.