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.

    Similar Reads

    PHP array_intersect_assoc: How it Works with Examples

    PHP array_intersect_assoc compares values and keys in arrays. It returns matches with the same key and value from all arrays…

    PHP filter_list(): List Available Filters

    PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…

    PHP array_filter: How to Filter Array Values with Examples

    You can use array_filter in PHP to remove unwanted data from arrays. It works with a custom callback or default…

    PHP array_change_key_case: Change Array Key Cases

    The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…

    PHP Null: How to Assign and Check for Null Values

    PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists.…

    PHP array_diff_ukey Function: How it Works with Examples

    The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…

    Learn How to Insert Documents in MongoDB Using PHP

    Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…

    PHP Anonymous Function: How It Works with Examples

    The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…

    Understanding the PHP Exclusive OR (XOR) Operator

    In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

    PHP Variadic Functions: Use Unlimited Arguments

    PHP Variadic functions show you a way to handle a variable number of arguments within a function. They are designed…

    Previous Article

    PHP Array Operators: Union, Equality, Identity

    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.