ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getMethods in PHP?
Returns ReflectionMethod objects for methods, optionally filtered by visibility or other method flags.
ReflectionClass::getMethods(?int $filter = ...)
Parameters:
$filter— Optional. An optional bitmask of ReflectionMethod or ReflectionProperty modifiers, depending on the method.
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->getMethods();
var_dump($result);
The output includes runtime-dependent object or source details, so it may differ across PHP installations.
Explanation: Returns ReflectionMethod objects for methods, optionally filtered by visibility or other method flags.
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->getMethods();
var_dump($result);
The output includes runtime-dependent object or source details, so it may differ across PHP installations.
Explanation: A ReflectionMethod filter bitmask can restrict which method descriptors are returned.
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->getMethods();
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 ReflectionMethod filter bitmask can restrict which method descriptors are returned.
- 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::getMethods | Returns ReflectionMethod objects for methods, optionally filtered by visibility or other method flags. |
ReflectionClass::getMethod() | getMethods() returns a list of reflected methods; getMethod() inspects a single named method. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getMethods() as though it executes the reflected class member. | A ReflectionMethod filter bitmask can restrict which method descriptors are returned. |
| Choosing ReflectionClass::getMethod() without checking whether it provides the same result. | getMethods() returns a list of reflected methods; getMethod() inspects a single named method. |
Key Takeaways
- Returns ReflectionMethod objects for methods, optionally filtered by visibility or other method flags.
- A ReflectionMethod filter bitmask can restrict which method descriptors are returned.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getMethods fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.