When building tools that inspect PHP code, ReflectionMethod::createFromMethodName() helps answer a focused question: creates ReflectionMethod from a fully qualified Class::method string.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionMethod::createFromMethodName()?
Creates ReflectionMethod from a fully qualified Class::method string.
Its method signature is:
public static function createFromMethodName(string $method): static
Parameters:
$method— Method name being reflected Required.
Return value: static
Example 1: Inspect a reflected method
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_METHOD)]
class ActionLabel { public function __construct(public string $label) {} }
interface Action { public function execute(string $message): string; }
abstract class AbstractAction { abstract public function perform(): void; }
class MailAction implements Action {
public function __construct(public string $name = 'Mail') {}
#[ActionLabel('send')]
public function execute(string $message): string { return $this->name . ': ' . $message; }
protected function verify(): bool { return true; }
private function internal(): string { return 'secret'; }
final public function finalize(): string { return 'complete'; }
public static function version(): int { return 2; }
public function __destruct() {}
}
class SmsAction extends MailAction { public function execute(string $message): string { return parent::execute($message); } }
class VerifiedAction extends MailAction { protected function verify(): bool { return false; } }
$result = ReflectionMethod::createFromMethodName("MailAction::execute");
echo $result->getName();
Output:
execute
Explanation: Creates ReflectionMethod from a fully qualified Class::method string.
Example 2: Compare an inherited method
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_METHOD)]
class ActionLabel { public function __construct(public string $label) {} }
interface Action { public function execute(string $message): string; }
abstract class AbstractAction { abstract public function perform(): void; }
class MailAction implements Action {
public function __construct(public string $name = 'Mail') {}
#[ActionLabel('send')]
public function execute(string $message): string { return $this->name . ': ' . $message; }
protected function verify(): bool { return true; }
private function internal(): string { return 'secret'; }
final public function finalize(): string { return 'complete'; }
public static function version(): int { return 2; }
public function __destruct() {}
}
class SmsAction extends MailAction { public function execute(string $message): string { return parent::execute($message); } }
class VerifiedAction extends MailAction { protected function verify(): bool { return false; } }
$result = ReflectionMethod::createFromMethodName("SmsAction::execute");
echo $result->getName();
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
execute
Result type: ReflectionMethod
Explanation: Only valid existing class and method names can be reflected.
Practical Example: Inspect application method metadata
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_METHOD)]
class ActionLabel { public function __construct(public string $label) {} }
interface Action { public function execute(string $message): string; }
abstract class AbstractAction { abstract public function perform(): void; }
class MailAction implements Action {
public function __construct(public string $name = 'Mail') {}
#[ActionLabel('send')]
public function execute(string $message): string { return $this->name . ': ' . $message; }
protected function verify(): bool { return true; }
private function internal(): string { return 'secret'; }
final public function finalize(): string { return 'complete'; }
public static function version(): int { return 2; }
public function __destruct() {}
}
class SmsAction extends MailAction { public function execute(string $message): string { return parent::execute($message); } }
class VerifiedAction extends MailAction { protected function verify(): bool { return false; } }
$result = ReflectionMethod::createFromMethodName("MailAction::version");
echo $result->getName();
// Use the reflection result in a simple decision.
if (is_bool($result)) {
echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
echo "\nEntries: " . count($result);
} elseif ($result === null) {
echo "\nNo value returned";
} else {
echo "\nReflection result available";
}
Output:
version
Reflection result available
Explanation: new ReflectionMethod accepts the class and method as separate parameters.
Important Behavior and Edge Cases
- Only valid existing class and method names can be reflected.
- new ReflectionMethod accepts the class and method as separate parameters.
- Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionMethod::createFromMethodName() | Creates ReflectionMethod from a fully qualified Class::method string. |
Related API | new ReflectionMethod accepts the class and method as separate parameters. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Only valid existing class and method names can be reflected. | Handle the specific limitation: Only valid existing class and method names can be reflected. |
| Confusing this method with Related API | new ReflectionMethod accepts the class and method as separate parameters. |
Key Takeaways
- Creates ReflectionMethod from a fully qualified Class::method string.
- Only valid existing class and method names can be reflected.
- new ReflectionMethod accepts the class and method as separate parameters.
Use ReflectionMethod::createFromMethodName() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.