DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AI Is Making PHP Cool Again
  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide

Trending

  • The Five Stages of AI Adoption: A Practical Maturity Model for Software Teams
  • Kubernetes Operations Playbook: The Essentials for Keeping Scale, Complexity, and Drift Under Control
  • Meta's New Muse Wonder Models Challenge OpenAI, but Access Comes With Limits
  • The English WHERE Clause: Plain-English Filters in Couchbase SQL++ Using Jev Model

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. An In-Depth Guide to PHP 8.1: Enums

An In-Depth Guide to PHP 8.1: Enums

PHP 8.1 added Enums, this allows us to set a defined number of possible values for an array.

By 
Razet Jain
·
Sep. 29, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Enums or enumerations are a new feature introduced in PHP 8.1 that contains a defined number of possible values you can use. When creating an app, you often come across scenarios where you have a predetermined list of options to select from, for instance:

  • A blog entry can be published, in the draft, or in review.
  • A player may be a medic, soldier, engineer, 
  • A ticket may be VIP, standing, or seated, 
  • and so on ...


Basic Syntax

We can create the Enum by using the enum keyword and defining the possible cases that are valid for this structure.

PHP
 
enum PostType {
  case Draft;
  case Published;
  case InReview;
}


Next, we can update our function that requires a PostType, but this time we're not just expecting an integer. Instead, we are telling our function to expect a variable of type PostType.

PHP
 
class Post {
  public function __construct(public string $title, public PostType $type){}
}


Pure vs Backed Enums

You may have noticed that using PostType::Published in a database query is not as straightforward as it once was because your database most likely wants to store this type as an integer (draft = 0, published = 1, etc.).

Enums have various flavors to accomplish it. In the preceding example, we simply defined a few potential cases without assigning a value to those options. They are referred to as pure enums.

If we want to be able to attach values to an enum, these are called backed enums, and they work pretty much in the same way.

Let's begin by adding values to our enum cases (we are attaching value and are setting the return type for the enum as well)

PHP
 
enum PostType: int {
  case Draft = 0;
  case Published = 1;
  case Unpublished = 2;
}


Now we can ask for the corresponding value for any of the cases in the enum by calling the value:

PHP
 
$post = new Post('Hello', PostType::Published);
echo "Post Type is: " . $post->type->value; // Post Type is: 1


Conclusion

Enums are a great addition to PHP for making it easier to come up with a data type that allows you to pick from a pre-defined set of choices with matching values.

PHP

Published at DZone with permission of Razet Jain. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • AI Is Making PHP Cool Again
  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook