ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::isInterface in PHP?
Reports whether the reflected type is an interface.
ReflectionClass::isInterface()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
interface HasIdentifier { public function id(): int; }
$reflectionClass = new ReflectionClass(HasIdentifier::class);
var_dump($reflectionClass->isInterface());
Output:
bool(true)
Explanation: Reports whether the reflected type is an interface.
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->isInterface();
var_dump($result);
Output:
bool(false)
Explanation: A class implementing an interface does not itself become an interface declaration.
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->isInterface();
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
- A class implementing an interface does not itself become an interface declaration.
- 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::isInterface | Reports whether the reflected type is an interface. |
ReflectionClass::implementsInterface() | isInterface() describes the reflected type itself; implementsInterface() checks a class against a specific interface. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing a class-declaration check by ReflectionClass::isInterface() with an invocation of the class itself. | A class implementing an interface does not itself become an interface declaration. |
| Choosing ReflectionClass::implementsInterface() without checking whether it provides the same result. | isInterface() describes the reflected type itself; implementsInterface() checks a class against a specific interface. |
Key Takeaways
- Reports whether the reflected type is an interface.
- A class implementing an interface does not itself become an interface declaration.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::isInterface fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.