Skip to content

Core\Reflect\ClassInfo::hasMethod

$classInfo->hasMethod(string $name): bool$classInfo->hasMethod(
    string $name,
): bool

Checks whether a class has a method with a given name.

hasMethod($name) returns true when the described class has a method called $name, and false when it has none. Methods the class inherits from a parent class count too. The name must match exactly, so Title does not find a method called title.

hasMethod also returns true for a private method. That method still cannot be called from outside its class. call throws a LogicError both for a private method and for a name that does not exist, so hasMethod is how a program tells the two cases apart.

The examples below check a few names, tell a private method from a missing one, and call an event handler only on the objects that have it.

Parameters

  • $namestringThe method's name, as the declaration writes it and as methods returns it.

Return value

Returns bool. true for a method of the class at any visibility. false if the class has no method with that name. So when a call fails, you can tell a private method from a misspelled name.

Examples

Check for a method by name

<?nvs
// Shows how to check whether a class has a method with a given name.
class Report
{
public function title(): string
{
return 'Monthly sales';
}
public function render(): string
{
return '<h1>Monthly sales</h1>';
}
}
var $found = Core\Reflect::forClass('Report');
if ($found != null) {
Core\Reflect\ClassInfo $info = $found;
// `hasMethod` returns `true` or `false`. This prints `yes`, `yes` and `no`.
array<string> $names = ['title', 'render', 'export'];
foreach ($names as string $name) {
echo $name, ': ', $info->hasMethod($name) ? 'yes' : 'no', "\n";
}
// Method names are case-sensitive, so this prints `no`.
echo 'Title: ', $info->hasMethod('Title') ? 'yes' : 'no', "\n";
}
Output
title: yes
render: yes
export: no
Title: no

A private method exists but cannot be called

<?nvs
// Shows the difference between a private method and a method that does not exist.
class Account
{
public function balance(): int
{
return 120;
}
private function resetPassword(): string
{
return 'reset';
}
}
class Try
{
public static function call(string $name): tainted string
{
var $info = Core\Reflect::forClass('Account');
if ($info == null) {
return 'no class';
}
// `hasMethod` returns `true` for a private method too.
if (!$info->hasMethod($name)) {
return $name . ': there is no such method';
}
try {
return $name . ': ' . ($info->call(new Account(), $name, []) as string);
} catch (LogicError $error) {
// `call` throws a `LogicError` for a private method and for a missing one.
// Only the `hasMethod` check above tells the two cases apart.
return $name . ': the method exists, but it is private';
}
}
}
// This prints one line for each name.
echo Try::call('balance'), "\n";
echo Try::call('resetPassword'), "\n";
echo Try::call('resetPasword'), "\n";
Output
balance: 120
resetPassword: the method exists, but it is private
resetPasword: there is no such method

Call an event handler only where it exists

<?nvs
// Shows how an event dispatcher calls a handler method only on the listeners that have one.
class AuditLog
{
public function onOrderPlaced(string $order): string
{
return 'audit: order ' . $order . ' was placed';
}
public function onOrderCancelled(string $order): string
{
return 'audit: order ' . $order . ' was cancelled';
}
}
class Mailer
{
public function onOrderPlaced(string $order): string
{
return 'mail: thank you for order ' . $order;
}
}
class Dispatcher
{
public static function dispatch(array<mixed> $listeners, string $event, string $order): void
{
// The event name `OrderPlaced` becomes the method name `onOrderPlaced`.
string $method = 'on' . $event;
foreach ($listeners as mixed $listener) {
var $info = Core\Reflect::forObject($listener);
// A listener without the method is skipped.
if ($info->hasMethod($method)) {
echo $info->call($listener, $method, [$order]) as string, "\n";
}
}
}
}
array<mixed> $listeners = [new AuditLog(), new Mailer()];
// Both listeners handle `OrderPlaced`. Only `AuditLog` handles `OrderCancelled`.
Dispatcher::dispatch($listeners, 'OrderPlaced', 'A-17');
Dispatcher::dispatch($listeners, 'OrderCancelled', 'A-18');
Output
audit: order A-17 was placed
mail: thank you for order A-17
audit: order A-18 was cancelled