314 of 410 menu

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

byenru