Reflection inspects class declarations, methods and properties at runtime.
The examples focus on the operation itself, including the important distinction: get_class_methods returns names but not the rich ReflectionMethod descriptors.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
Read the input requirements before applying this pattern to production data.
Example 1: Basic Use
Reflection enumerates declarations; inspecting them does not execute a method.
<?php
class Purchase { public function __construct(public int $id) {} public function total(): float { return 19.5; } }
$type = new ReflectionClass(Purchase::class);
foreach ($type->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
echo $method->getName() . PHP_EOL;
}
Output from a successful local PHP 8.4 run:
__construct
total
Example 2: Checking Behavior
Reflection enumerates declarations; inspecting them does not execute a method. The reflected member selection changes without executing class business methods.
<?php
class Purchase { public function __construct(public int $id) {} public function total(): float { return 19.5; } }
$type = new ReflectionClass(Purchase::class);
foreach ($type->getProperties(ReflectionProperty::IS_PUBLIC) as $method) {
echo $method->getName() . PHP_EOL;
}
Output from a successful local PHP 8.4 run:
id
Example 3: Practical Example
Reflection enumerates declarations; inspecting them does not execute a method. The reflected member selection changes without executing class business methods.
<?php
class Purchase { public function __construct(public int $id) {} public function total(): float { return 19.5; } }
$type = new ReflectionClass(Purchase::class);
foreach ($type->getMethods() as $method) {
echo $method->getName() . PHP_EOL;
}
Output from a successful local PHP 8.4 run:
__construct
total
Important Behavior and Edge Cases
Reflection reports metadata; invoking reflected methods still executes application code and may have side effects.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Using Reflection to Inspect PHP Classes | Reflection inspects class declarations, methods and properties at runtime. |
| Related API or alternative | get_class_methods returns names but not the rich ReflectionMethod descriptors. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Reflection reports metadata; invoking reflected methods still executes application code and may have side effects. |
| Confusing it with a related API | get_class_methods returns names but not the rich ReflectionMethod descriptors. |
Key Takeaways
- Reflection inspects class declarations, methods and properties at runtime.
- Reflection reports metadata; invoking reflected methods still executes application code and may have side effects.
- get_class_methods returns names but not the rich ReflectionMethod descriptors.
Use Using Reflection to Inspect PHP Classes when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.