PHP | ReflectionClass getMethods() Function

The ReflectionClass::getMethods() function is an inbuilt function in PHP which is used to return an array of specified methods.

Syntax:

array ReflectionClass::getMethods( int $filter )

Parameters: This function accepts a single parameter filter which is used to remove some of the methods.

Return Value: This function returns an array of specified methods.

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



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Initialising a user-defined Class
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 getMethods() function
$methods = $class->getMethods();
  
// Getting an array of specified methods
var_dump($methods);
?>

chevron_right


Output:

array(5) {
  [0]=>
  object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(3) "CSE"
    ["class"]=>
    string(11) "Departments"
  }
  [1]=>
  object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(3) "ECE"
    ["class"]=>
    string(11) "Departments"
  }
  [2]=>
  object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(2) "EE"
    ["class"]=>
    string(11) "Departments"
  }
  [3]=>
  object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(2) "IT"
    ["class"]=>
    string(11) "Departments"
  }
  [4]=>
  object(ReflectionMethod)#6 (2) {
    ["name"]=>
    string(9) "Mechnical"
    ["class"]=>
    string(11) "Departments"
  }
}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Initialising a user-defined Class
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 getMethods() function
$methods = $class->getMethods(ReflectionMethod::IS_STATIC);
  
// Getting an array of specified methods
var_dump($methods);
?>

chevron_right


Output:

array(2) {
  [0]=>
  object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(2) "EE"
    ["class"]=>
    string(11) "Departments"
  }
  [1]=>
  object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(2) "IT"
    ["class"]=>
    string(11) "Departments"
  }
}

Reference: https://www.php.net/manual/en/reflectionclass.getmethods.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.