ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::isEnum in PHP?
Reports whether the reflected type is an enum.
ReflectionClass::isEnum() : bool
This operation does not accept method arguments.
Return type: bool. Native enum declarations are recognized; classes containing constants are not enums.
Example 1: Understanding the Basic Behavior
<?php
enum RecordStatus { case Pending; case Sent; }
$reflectionClass = new ReflectionClass(RecordStatus::class);
var_dump($reflectionClass->isEnum());
Output:
bool(true)
Explanation: Reports whether the reflected type is an enum.
Example 2: Testing Another Case
<?php
interface Exportable { public function export(): string; }
trait HasId { public function getId(): int { return 12; } }
class Invoice implements Exportable { use HasId; public string $name = 'Invoice'; public const STATE = 'open'; public function export(): string { return 'invoice'; } }
$reflectionClass = new ReflectionClass(Invoice::class);
$result = $reflectionClass->isEnum();
var_dump($result);
Output:
bool(false)
Explanation: Native enum declarations are recognized; classes containing constants are not enums.
Practical Example: Applying the Operation
<?php
abstract class BaseJob { protected string $name = 'job'; abstract public function run(): void; }
class CompletedJob extends BaseJob { public function run(): void {} }
$reflectionClass = new ReflectionClass(CompletedJob::class);
$result = $reflectionClass->isEnum();
var_dump($result);
Output:
bool(false)
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- Native enum declarations are recognized; classes containing constants are not enums.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionClass::isEnum | Reports whether the reflected type is an enum. |
enum_exists() | isEnum() checks the reflected type; enum_exists() looks up an enum by its name. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing a class-declaration check by ReflectionClass::isEnum() with an invocation of the class itself. | Native enum declarations are recognized; classes containing constants are not enums. |
| Choosing enum_exists() without checking whether it provides the same result. | isEnum() checks the reflected type; enum_exists() looks up an enum by its name. |
Key Takeaways
- Reports whether the reflected type is an enum.
- Native enum declarations are recognized; classes containing constants are not enums.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::isEnum fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.