Enums can implement interfaces and define methods that place behavior alongside their cases.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Enums with Methods and Interfaces Works
Enums can implement interfaces and define methods that place behavior alongside their cases.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
enum AccessLevel { case Reader; case Writer; public function canEdit(): bool { return $this === self::Writer; } }
var_dump(AccessLevel::Writer->canEdit());
Output:
bool(true)
Enums can implement interfaces and define methods that place behavior alongside their cases.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
interface PrintableStatus { public function description(): string; }
enum ApprovalStatus implements PrintableStatus { case Pending; case Approved; public function description(): string { return strtolower($this->name); } }
echo ApprovalStatus::Approved->description();
Output:
approved
Do not use mutable instance properties on enum cases; an enum represents a fixed singleton case.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
interface LabeledState { public function label(): string; }
enum PublicationState implements LabeledState { case Draft; case Published; public function label(): string { return match($this) { self::Draft => "Not live", self::Published => "Live" }; } }
echo PublicationState::Published->label();
Output:
Live
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Do not use mutable instance properties on enum cases; an enum represents a fixed singleton case.
- Enums can implement interfaces and define methods that place behavior alongside their cases.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Enums with Methods and Interfaces | Enums can implement interfaces and define methods that place behavior alongside their cases. |
| Alternative approach | match expressions outside an enum work too, but enum methods keep case-dependent behavior near the type. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Enums with Methods and Interfaces | Do not use mutable instance properties on enum cases; an enum represents a fixed singleton case. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | match expressions outside an enum work too, but enum methods keep case-dependent behavior near the type. |
Key Takeaways
- Enums can implement interfaces and define methods that place behavior alongside their cases.
- Do not use mutable instance properties on enum cases; an enum represents a fixed singleton case.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Enums with Methods and Interfaces is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.