Enum backed values
📖 Scenario: You are creating a simple program to represent different user roles in a system. Each role has a specific numeric code that the system uses internally.
🎯 Goal: Build a PHP enum with backed integer values for user roles, then print the numeric code of a specific role.
📋 What You'll Learn
Create a backed enum called
UserRole with integer valuesInclude roles:
Admin = 1, Editor = 2, Viewer = 3Create a variable called
role and assign it the UserRole::EditorPrint the integer value of the
role variable💡 Why This Matters
🌍 Real World
Enums with backed values are used in real systems to represent fixed sets of options with specific codes, like user roles, status codes, or categories.
💼 Career
Understanding enums helps you write clearer, safer code that is easier to maintain and less error-prone, a valuable skill for PHP developers.
Progress0 / 4 steps