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

phpmethod_exists(object|string $class, string $method): bool

Example

Let's check the existence of a method in a class:

php<?php
  class MyClass {
    public function test() {}
  }

  $res = method_exists('MyClass', 'test');
  var_dump($res);
?>

Code execution result:

phptrue

Example

Let's check the existence of a method in an object:

php<?php
  $obj = new MyClass();
  $res = method_exists($obj, 'test');
  var_dump($res);
?>

Code execution result:

phptrue

Example

Let's check for a non-existent method:

php<?php
  $res = method_exists('MyClass', 'notExists');
  var_dump($res);
?>

Code execution result:

phpfalse

See Also