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
The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…
The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…
In this tutorials, we are going to explain the PHP MySQL Delete Data process with a simple, unique guide. By…
Echo and print are foundational in displaying output in PHP, and though they might be similar in some ways, they…
PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…
In regard to the use of constants in PHP, clear understanding of their definition and use could be critical in…
PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…
It’s very important to remember user data for each session when building web applications. This enables a high level of…
The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…
You may need to cut the last character from a string if it shows by mistake in PHP. That may…