What is the get_class_methods in PHP?

The get_class_methods method can be used to get the name of the list of methods available in the class.

Syntax


get_class_methods(object|string $object_or_classname): array

Arguments

We can pass the class name or object instance as an argument.

Returns

This method will return an array.

Code

<?php
class MyClass {
public $name;
// constructor
function __construct($name) {
$this->name = $name;
}
//static function
public static function staticFn() {
echo "Static Function!";
}
// normal function
function set($n) {
$this->name = $n;
}
function get(){
return $this->name;
}
}
echo "Getting class method using class name\n";
print_r(get_class_methods('MyClass'));
echo "\nGetting class method using object\n";
print_r(get_class_methods(new MyClass("PHP")));
?>

Explanation

In the code above:

  • We created a MyClass class with a constructor, methods, and static method.

  • We first called the get_class_methods function with the class name as an argument, then used the MyClass object as an argument. Both the function calls will return an array with the name of the available methods in the MyClass class.

Free Resources