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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Trending

  • Streamlining Event Data in Event-Driven Ansible
  • GDPR Compliance With .NET: Securing Data the Right Way
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Emerging Data Architectures: The Future of Data Management
  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 user avatar
Razet Jain
·
Sep. 29, 22 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!