Content · Web Development · PHP

PHP get_class_methods() Function: Usage and Examples

PHP get_class_methods() Function: Usage and Examples: Lists methods accessible from the current scope. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

The PHP get_class_methods API performs a focused operation on an object or value. The examples below show its effect on actual data.

Lists methods accessible from the current scope.

What Is get_class_methods in PHP?


Lists methods accessible from the current scope.

Syntax:

get_class_methods(object|string $object_or_class): array

The method accepts the following parameters:

  • $object_or_class — Required. A object|string parameter named object_or_class; the examples below show a supported value.

Return type declared by PHP: array. The value or side effect is demonstrated in the examples below.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

class TaskRunner { public function start(): void {} private function reset(): void {} }
print_r(get_class_methods(TaskRunner::class));

Output:

Array
(
    [0] => start
)

Lists methods accessible from the current scope.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

class TaskRunner { public function start(): void {} private function reset(): void {} }
print_r(get_class_methods(TaskRunner::class));
// The output reflects definitions in this PHP process.

Output:

Array
(
    [0] => start
)

Visibility affects which methods appear in the result.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

class TaskRunner { public function start(): void {} private function reset(): void {} }
print_r(get_class_methods(TaskRunner::class));
// Use this result while deciding how to handle optional application features.

Output:

Array
(
    [0] => start
)

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Visibility affects which methods appear in the result.
  • Lists methods accessible from the current scope.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
get_class_methodsLists methods accessible from the current scope.
get_class_vars()get_class_methods() lists accessible methods; get_class_vars() lists visible default properties.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of get_class_methodsVisibility affects which methods appear in the result.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyget_class_methods() lists accessible methods; get_class_vars() lists visible default properties.

Key Takeaways


  • Lists methods accessible from the current scope.
  • Visibility affects which methods appear in the result.
  • Use the example that matches the values, runtime version and dependencies in your application.

get_class_methods is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.