Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

class
methods
communitycreator

What is the get_class_methods in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

class
methods
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring