The method_exists Function
The method_exists
function checks whether the specified method
exists in the given class or object. The first parameter accepts
an object or class name, and the second - the method name as a string.
Returns true
if the method exists, and false
otherwise.
Syntax
method_exists(object|string $class, string $method): bool
Example
Let's check the existence of a method in a class:
<?php
class MyClass {
public function test() {}
}
$res = method_exists('MyClass', 'test');
var_dump($res);
?>
Code execution result:
true
Example
Let's check the existence of a method in an object:
<?php
$obj = new MyClass();
$res = method_exists($obj, 'test');
var_dump($res);
?>
Code execution result:
true
Example
Let's check for a non-existent method:
<?php
$res = method_exists('MyClass', 'notExists');
var_dump($res);
?>
Code execution result:
false
See Also
-
the property_exists function,
which checks for the existence of a property -
the function_exists function,
which checks for the existence of a function -
the get_class_methods function,
which returns class methods -
the is_callable function,
which checks callability