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 continue Statement: How to Skip Steps in Iteration

    The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

    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…

    How to Delete Data in PHP MySQL

    In this tutorials, we are going to explain the PHP MySQL Delete Data process with a simple, unique guide. By…

    PHP echo vs print: Key Differences & Usage Guide

    Echo and print are foundational in displaying output in PHP, and though they might be similar in some ways, they…

    Inheritance in PHP: Share Code from Class to Class & Examples

    PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…

    Understanding PHP Constants: A Simple Guide with Examples

    In regard to the use of constants in PHP, clear understanding of their definition and use could be critical in…

    PHP array_intersect_uassoc: How it Works with Examples

    PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…

    PHP $_SESSION: Secure Your Web Applications in PHP

    It’s very important to remember user data for each session when building web applications. This enables a high level of…

    PHP OOP Constructor: How It Works in a Class with Examples

    The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

    How to Remove the Last Character from a String in PHP

    You may need to cut the last character from a string if it shows by mistake in PHP. That may…

    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.