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
<?phpclass MyClass {public $name;// constructorfunction __construct($name) {$this->name = $name;}//static functionpublic static function staticFn() {echo "Static Function!";}// normal functionfunction 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
MyClassclass with a constructor, methods, and static method. -
We first called the
get_class_methodsfunction with the class name as an argument, then used theMyClassobject as an argument. Both the function calls will return an array with the name of the available methods in theMyClassclass.