ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getMethod in PHP?
Returns a ReflectionMethod for a method by name, throwing ReflectionException when it does not exist.
ReflectionClass::getMethod(string $name)
Parameters:
$name— Required. The exact class-member name (method, property or constant) to inspect.
Example 1: Understanding the Basic Behavior
<?php
class Report { public const VERSION = 2; public string $title = 'Draft'; protected int $pages = 10; public static string $category = 'docs'; public function publish(): void {} }
$reflectionClass = new ReflectionClass(Report::class);
$result = $reflectionClass->getMethod("publish");
var_dump($result);
The output includes runtime-dependent object or source details, so it may differ across PHP installations.
Explanation: Returns a ReflectionMethod for a method by name, throwing ReflectionException when it does not exist.
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->getMethod("export");
var_dump($result);
The output includes runtime-dependent object or source details, so it may differ across PHP installations.
Explanation: A missing method throws ReflectionException; check hasMethod() when appropriate.
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->getMethod("run");
var_dump($result);
The output includes runtime-dependent object or source details, so it may differ across PHP installations.
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 missing method throws ReflectionException; check hasMethod() when appropriate.
- 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::getMethod | Returns a ReflectionMethod for a method by name, throwing ReflectionException when it does not exist. |
ReflectionClass::hasMethod() | getMethod() returns a ReflectionMethod or throws for a missing method; hasMethod() returns a boolean. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getMethod() as though it executes the reflected class member. | A missing method throws ReflectionException; check hasMethod() when appropriate. |
| Choosing ReflectionClass::hasMethod() without checking whether it provides the same result. | getMethod() returns a ReflectionMethod or throws for a missing method; hasMethod() returns a boolean. |
Key Takeaways
- Returns a ReflectionMethod for a method by name, throwing ReflectionException when it does not exist.
- A missing method throws ReflectionException; check hasMethod() when appropriate.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getMethod fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.