Today we will look at one of the functions that predates the powerful reflection API. That said, there are situations where get_class_methods will be faster.

The documentation states that get_class_methods returns the class method (that is, function) names. This is mostly true - what it returns are the methods available to the current scope. Protected or private methods not visible to the function scope where get_class_methods was called won't be returned as seen in the example below:

Code php:
class A {
  public function foo() {
    print_r(get_class_methods($this));
  }
 
  protected function moo() {}
 
  private function zoo() {}
}
 
class B extends A {
  public function bar() {
    print_r(get_class_methods($this));
  }
 
  private function car() {}
 
}
 
$a = new A();
 
print_r(get_class_methods($a)); // Returns foo
$a->foo(); // Returns foo, moo, zoo
 
$b = new B();
print_r(get_class_methods($b)); // Returns foo, bar
$b->foo(); // Returns foo, moo, zoo, bar (scope of A::foo )
$b->bar(); // Returns foo, moo, bar, car (scope of B::bar )

As you first start working with protected and private methods get_class_methods can help establish what is and is not visible at each scope which used in debugging.

You can of course use it to test to see if an object has a method before you try to invoke it, but this is what interfaces are for.

Code php:
interface C {
  public function foo();
}
 
class A implements C {
  public function foo() {}
}
 
$a = new A();
 
if ($a instanceof C) {
  $a->foo();
}

Questions, comments, corrections?