PHP | ReflectionClass hasMethod() Function

The ReflectionClass::hasMethod() function is an inbuilt function in PHP which is used to check the specified method is present or not.

Syntax:

bool ReflectionClass::hasMethod( string $name )

Parameters: This function accepts a single parameter $name which holds the name of the method which are being checked.

Return Value: This function returns TRUE if the specified method is present, otherwise returns FALSE.

Below programs illustrate the ReflectionClass::hasMethod() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Initialising a user-defined Class Departments
class Departments {
    public function CSE() { }
    final protected function ECE() { }
    private static function EE() { }
    static function IT() { }
    private function Mechnical() { }
}
  
// Using ReflectionClass() over the
// user-defined class Departments
$class = new ReflectionClass('Departments');
  
// Calling the hasMethod() function
$methods = $class->hasMethod('CSE');
  
// Getting the value true or false
var_dump($methods);
?>

chevron_right


Output:

bool(true)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Initialising a user-defined Class Company
class Company {
    public function GeeksforGeeks() { }
    static function gfg() { }
}
   
// Using ReflectionClass() over the
// user-defined class Company
$class = new ReflectionClass('Company');
   
// Calling the hasMethod() function
$methods = $class->hasMethod('TCS');
   
// Getting the value true or false
var_dump($methods);
?>

chevron_right


Output:

bool(false)

Reference: https://php.net/manual/en/reflectionclass.hasmethod.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.