get_class_methods

Returns the names of the methods a class defines that are visible from the calling scope.

Signature
get_class_methods(object|string $object_or_class): array
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$object_or_classobject|stringAn object instance or the name of an existing class.

Return value

arrayReturns an array of method names defined for the class specified by object_or_class, as a list of strings.

Errors

Throws a TypeError when object_or_class is neither an object nor the name of an existing class.

Changelog

  • PHP 8.0.0The object_or_class parameter now only accepts objects or valid class names. Passing a name that does not resolve to a class is an error rather than returning an empty result.

get_class_methods() lists a class's method names, including inherited ones and static methods, as a flat array of strings. Names are returned in their declared case and in declaration order (own methods first, then inherited ones).

The scope rule

This is the detail that surprises everyone: the result depends on where the call happens. Called from outside the class you get public methods only. Called from inside a method of the same class you also get its protected and private methods, and from a subclass you get the public and protected ones. So the same call can legitimately return different arrays in two places in your codebase.

When to reach for it

It fits lightweight plugin and dispatcher patterns — collecting every handle* method on a controller, or building a list of assertions on a test class — without pulling in the Reflection API. It is also useful in debugging output when you want to see what an object actually exposes.

When you need more than names — visibility, parameters, return types, attributes — switch to ReflectionClass::getMethods(). And to test one specific method, method_exists() or is_callable() is cheaper and clearer than scanning this array.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Auto-register every public handle* method on a controller as a route action
  • Dump an object's public API in a debug or REPL helper
  • Verify in a test that a class exposes the methods an interface expects

Frequently asked questions

Does get_class_methods include inherited and static methods?

Yes to both. Inherited methods appear after the class's own methods, and static methods are listed alongside instance methods with no marker distinguishing them.

Why does get_class_methods not show private methods?

Because it respects the calling scope. Call it from inside the class (for example with $this) to see private and protected methods, or use ReflectionClass::getMethods() with the visibility filters you want.

What is the difference between get_class_methods and method_exists?

method_exists() checks one name and ignores visibility, returning true even for private methods. get_class_methods() returns the whole list but only the parts visible from where you called it.

More examples

Scope changes the answer

The first call shows two public methods, the second shows all four including the private key(), and the third shows three — everything but the private method, which a subclass cannot see.

Example · php
<?php
class Vault {
    public function open(): void {}
    protected function unlock(): void {}
    private function key(): void {}

    public function insideView(): array { return get_class_methods($this); }
}
class SubVault extends Vault {
    public function subView(): array { return get_class_methods($this); }
}

print_r(get_class_methods('Vault'));      // outside
print_r((new Vault())->insideView());     // inside the class
print_r((new SubVault())->subView());     // inside a subclass

Collecting handler methods for a dispatcher

Filtering by prefix turns the method list into a command table with no configuration. helper() is skipped because it does not match the prefix, not because of visibility.

Example · php
<?php
class Commands {
    public function handleGreet(string $n): string { return "hi $n"; }
    public function handleShout(string $n): string { return strtoupper($n) . '!'; }
    public function helper(): void {}
}

$obj = new Commands();
foreach (get_class_methods($obj) as $m) {
    if (str_starts_with($m, 'handle')) {
        echo strtolower(substr($m, 6)), ' => ', $obj->$m('ada'), "\n";
    }
}

Discussion

  • Be the first to comment on this lesson.