method_exists
Checks whether a class or an object has a method.
Syntax
method_exists(object|string $object_or_class, string $method): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $object_or_class | object|string | An object or a class name. |
| $method | string | The method name. |
Return value
bool`true` if the method exists.
Examples
An existing and a missing method:
<?php
$date = new DateTime();
var_dump(method_exists($date, 'format'));
var_dump(method_exists($date, 'unknown'));Output:
bool(true)
bool(false)Notes
The function finds private methods too, which cannot be called from outside. To find out whether a method can be called, use is_callable([$object, 'method']).
See also
- property_exists — Checks whether a class or an object has a property.
- class_exists — Checks whether a class is defined.
- get_class — Returns the class name of an object.
In the tutorial
- Classes and objects — What classes and objects are in PHP, how to describe properties and methods and create objects.