method_exists
Checks whether a named method is defined on a given object or class.
method_exists(object|string $object_or_class, string $method): bool- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$object_or_class | object|string | An object instance or a class name. |
$method | string | The method name. Matching is case-insensitive. |
Return value
booltrue if the method is defined for the given object or class, false otherwise.
Errors
Throws a TypeError if the first argument is neither an object nor a string; an unknown class name returns false after autoloading is attempted.
Changelog
- PHP 7.4.0Checking a class against an inherited private method now returns false.
method_exists() tells you whether a method is declared. It says nothing about whether you can call it. Private and protected methods return true just like public ones, so a guard written as if (method_exists($obj, 'reset')) { $obj->reset(); } will happily fall into a fatal Error when reset() turns out to be protected.
For "can I call this from here?", use is_callable([$obj, 'reset']), which respects visibility from the calling scope and also returns true for methods routed through __call().
When to reach for it
- Optional lifecycle hooks: run
$plugin->boot()only if the plugin declared one. - Version-tolerant code against a library whose newer releases added a method.
- Deciding between two APIs on the same object, for example preferring a
toArray()overjsonSerialize().
What trips people up
Magic methods are invisible to it. A class with __call() answers to any method name at runtime, yet method_exists() returns false for all of them — the usual cause of "my proxy object fails the check but works fine".
Passing a class name triggers autoloading, and static and instance methods are not distinguished: both report true whether you pass the object or the class string.
Example
When to use it
- Calling an optional boot() or shutdown() hook on plugin objects that may not define one.
- Supporting two versions of a library where a method was only added in the newer release.
- Choosing between toArray() and jsonSerialize() when normalising a value object.
Frequently asked questions
Does method_exists work with __call?
No. Methods handled by __call() or __callStatic() are not declared, so method_exists() returns false for them. Use is_callable(), which accounts for the magic methods.
Does method_exists find private methods?
Yes, when called on an object or its own class it returns true for private and protected methods. Since PHP 7.4, a class check against a private method inherited from a parent returns false.
Is method_exists case sensitive?
No. PHP method names are case-insensitive, so method_exists($obj, 'RENDER') matches a method declared as render().
More examples
method_exists versus is_callable
open() is both. lock() exists but is not callable from outside the class. missing is neither. The middle row is the case that turns a naive method_exists() guard into a fatal error.
<?php
class Box {
public function open(): string { return 'opened'; }
protected function lock(): void {}
}
$b = new Box();
foreach (['open', 'lock', 'missing'] as $m) {
printf("%-8s exists=%s callable=%s\n", $m,
var_export(method_exists($b, $m), true),
var_export(is_callable([$b, $m]), true));
}An optional lifecycle hook
Only the first plugin is booted; the second takes the fallback branch. get_class_methods() lists the public methods visible from the calling scope, which is a useful companion when debugging why a check failed.
<?php
class WithBoot { public function boot(): void { echo "booted\n"; } }
class WithoutBoot {}
foreach ([new WithBoot(), new WithoutBoot()] as $plugin) {
if (method_exists($plugin, 'boot')) {
$plugin->boot();
} else {
echo get_class($plugin), " has no boot()\n";
}
}
print_r(get_class_methods('WithBoot'));
Discussion