Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

method_exists

Checks whether a class or an object has a method.

Syntax

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

Parameters

ParameterTypeDescription
$object_or_classobject|stringAn object or a class name.
$methodstringThe 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

In the tutorial

  • Classes and objects — What classes and objects are in PHP, how to describe properties and methods and create objects.

Thanks for reading!

Donate to the site — we work hard for you.