PHP get_class_methods() Function

What is PHP get_class_methods() Function?

If you want to get all the methods of a class or the object of a class, use PHP get_class_methods() function.

It returns the method names in an array.

Syntax:

get_class_methods(class_or_object)

Parameters:

The Function has 1 parameter which is required-

class_or_object (Required): It specifies either a class name (Check example 1) or an object of an object (Check example 2).

Return Values:

The function returns-

  • an array of the methods, on success. Check example 1.
  • NULL, if there occurs an error (ex. class not found).

Examples:

Example 1:

<pre>
<?php
   class Human {
      function __construct(){}
      public function eat() {}
      public function sleep() {}
   }
   $methods_names = get_class_methods('Human');
   echo "The class 'Human' has the following methods- <br />";
   print_r($methods_names);
?>
</pre>

Output:

The class 'Human' has the following methods- 
Array
(
[0] => __construct
[1] => eat
[2] => sleep
)

Example 2:

<pre>
<?php
   class Human {
      function __construct(){}
      public function eat() {}
      public function sleep() {}
   }
   $objHuman = new Human();
   $methods_names = get_class_methods($objHuman);
   echo "The class 'Human' has the following methods- <br />";
   print_r($methods_names);
?>
</pre>

Output:

The class 'Human' has the following methods- 
Array
(
[0] => __construct
[1] => eat
[2] => sleep
)

Notes on get_class_methods() Function:

  • This function only returns public methods. So, private and protected methods are not included. Check following example-
  • <pre>
    <?php
       class Human {
          public function eat() {}
          private function walk() {}
          protected function run() {}
          public function sleep() {}
       }
       $methods_names = get_class_methods('Human');
       echo "The class 'Human' has the following methods- <br />";
       print_r($methods_names);
    ?>
    </pre>
    

    Output:

    The class 'Human' has the following methods- 
    Array
    (
    [0] => eat
    [1] => sleep
    )
  • The function not only displays the methods from itself but also from its parent classes too. (Check example 4). Check following example-
  • <pre>
    <?php
       class Human {
          public function eat() {}
          public function walk() {}
          public function sleep() {}
       }
    
       class Male extends Human {
          public function male_func() {}
       }
       $methods_names = get_class_methods('Male');
       print_r($methods_names);
    ?>
    </pre>
    

    Output:

    The class 'Human' has the following methods- 
    Array
    (
    [0] => male_func
    [1] => eat
    [2] => walk
    [3] => sleep
    )
  • If you call the function inside a class, it also displays the private and protected methods. Check following example-
  • <pre>
    <?php
    class Human {
          function __construct(){
             echo "The class 'Human' has the following methods- <br />";
             print_r(get_class_methods('Human'));
          }
          public function eat() {}
          private function walk() {}
          protected function run() {}
          public function sleep() {}
       }
       $objHuman = new Human();
    ?>
    </pre>
    

    Output:

    The class 'Human' has the following methods- 
    Array
    (
    [0] => __construct
    [1] => eat
    [2] => walk
    [3] => run
    [4] => sleep
    )

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP get_class_methods() Function

get_class_vars() is a class/object type function in PHP. It is a useful function to see the list of method a class contains.

Reference:

https://www.php.net/manual/en/function.get-class-methods.php