ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::hasMethod in PHP?
Checks whether a named method exists, including inherited methods.
ReflectionClass::hasMethod(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->hasMethod("publish");
var_dump($result);
Output:
bool(true)
Explanation: Checks whether a named method exists, including inherited methods.
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->hasMethod("export");
var_dump($result);
Output:
bool(true)
Explanation: Inherited methods can make hasMethod() true even if the method is not declared directly on the class.
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->hasMethod("run");
var_dump($result);
Output:
bool(true)
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- Inherited methods can make hasMethod() true even if the method is not declared directly on the class.
- 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::hasMethod | Checks whether a named method exists, including inherited methods. |
ReflectionClass::getMethod() | hasMethod() checks existence without throwing; getMethod() returns a descriptor or throws if absent. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing a class-declaration check by ReflectionClass::hasMethod() with an invocation of the class itself. | Inherited methods can make hasMethod() true even if the method is not declared directly on the class. |
| Choosing ReflectionClass::getMethod() without checking whether it provides the same result. | hasMethod() checks existence without throwing; getMethod() returns a descriptor or throws if absent. |
Key Takeaways
- Checks whether a named method exists, including inherited methods.
- Inherited methods can make hasMethod() true even if the method is not declared directly on the class.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::hasMethod fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.