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_count_values: How it Works with Examples

    Developers use the array_count_values function in PHP to find how many times each value appears in an array. What is…

    PHP array_key_first Function: How it Works with Examples

    The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

    PHP strtolower Function: Convert Strings to Lowercase

    Text does not always look the same. One word may appear in lowercase, another in uppercase. You want both to…

    Constants in PHP: How They Work & Examples

    Constants in PHP store fixed values that don’t change during execution. We will cover the following topics in this article:…

    How to Insert Multiple Rows in MySQL with PHP?

    Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…

    PHP Array: Accessing and Managing Elements

    PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…

    PHP array_diff_uassoc: How to Compare Arrays with Keys

    PHP array_diff_uassoc compares arrays by values and keys. You can also pass a custom function to compare keys in your…

    PHP MySQL LIMIT Data: How to Optimize PHP Queries?

    There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…

    PHP array_column: How to Extract Values from Arrays

    The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…

    IF-Else: Executes the IF-Else Statement in PHP

    Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if…

    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.