ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::isFinal in PHP?
Reports whether the class is declared final.
ReflectionClass::isFinal()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
final class CompletedRecord {}
$reflectionClass = new ReflectionClass(CompletedRecord::class);
var_dump($reflectionClass->isFinal());
Output:
bool(true)
Explanation: Reports whether the class is declared final.
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->isFinal();
var_dump($result);
Output:
bool(false)
Explanation: A final class cannot be subclassed, but its objects can still be instantiated if otherwise allowed.
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->isFinal();
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 final class cannot be subclassed, but its objects can still be instantiated if otherwise allowed.
- 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::isFinal | Reports whether the class is declared final. |
ReflectionClass::getModifiers() | isFinal() returns a boolean; getModifiers() returns a bitmask for several class modifiers. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing a class-declaration check by ReflectionClass::isFinal() with an invocation of the class itself. | A final class cannot be subclassed, but its objects can still be instantiated if otherwise allowed. |
| Choosing ReflectionClass::getModifiers() without checking whether it provides the same result. | isFinal() returns a boolean; getModifiers() returns a bitmask for several class modifiers. |
Key Takeaways
- Reports whether the class is declared final.
- A final class cannot be subclassed, but its objects can still be instantiated if otherwise allowed.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::isFinal fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.