Friday, September 5, 2025
HomeLanguagesPHP method_exists() Function

PHP method_exists() Function

The method_exists() function is an inbuilt function in PHP which used to check the class method exists or not. It returns “true” if the method exists otherwise returns “false”.

Syntax:

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

Parameters: This function accepts two parameters that are described below:

  • $object_or_class: It contains the name of the object or class.
  • method: It contains the name of the method.

Return Value: This method returns “true” if the class method exists in a given object or class, and false otherwise.

Example 1: In this example, we will check method_exists() in a given class or object, if not exists it will return “true”.

PHP




<?php
$directory = new Directory('.') ;
var_dump(method_exists($directory,'read'));
?>


Output:

bool(true);

Example 2:  In this example, we will check for “redirect” in method_exists() in a given class or object, if not it will return “false”.

PHP




<?php
$directory = new Directory('.') ;
var_dump(method_exists($directory,'redirect'));
?>


Output:

bool(false);

Reference: https://www.php.net/manual/en/function.method-exists.php

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS