method_exists()

method_exists() — Checks if the class method exists.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Checks if the class method exists in the given object_or_class.

Signature

method_exists(object|string $object_or_class, string $method): bool

Parameters

ParameterTypeWhat it takes
$object_or_classobject|stringAn object instance or a class name
$methodstringThe method name

Return value

Returns true if the method given by method has been defined for the given object_or_class, false otherwise.

Example

A runnable snippet:

$directory = new Directory('.'); var_dump(method_exists($directory,'read'));

Output:

bool(true)

Changelog

VersionChange
7.4.0Class checks against inherited private methods now return false.

See also

function_exists(), is_callable(), class_exists()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page method_exists() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.