Skip to content
← Back to Snippets
Code

Get Class Methods

Returns the public methods available on an existing PHP class name after confirming the class exists.

Purpose

Returns the public methods available on an existing PHP class name after confirming the class exists.

Snippet details

ContextUtilityLevelProductionCopy-and-paste statusMarked safe after review.

Categories

  • Forms and Validation

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Get Class Methods.
 *
 * Purpose:
 * Reads the public methods exposed by an available PHP class name.
 *
 * @param string $class_name Class name to inspect.
 * @return array Method lookup details.
 */
function ogSnippetGetClassMethods(string $class_name): array {
	$class_name = trim($class_name);

	if ($class_name === '') {
		return array(
			'class_name' => '',
			'found' => false,
			'methods' => array(),
			'count' => 0
		);
	}

	if (class_exists($class_name, false) === false) {
		return array(
			'class_name' => $class_name,
			'found' => false,
			'methods' => array(),
			'count' => 0
		);
	}

	$methods = get_class_methods($class_name);

	if (is_array($methods) === false) {
		$methods = array();
	}

	sort($methods);

	return array(
		'class_name' => $class_name,
		'found' => true,
		'methods' => $methods,
		'count' => count($methods)
	);
}

$method_report = ogSnippetGetClassMethods('DateTimeImmutable');

echo 'DateTimeImmutable method count: '.$method_report['count'];
echo "\n";
echo 'Expanse navigation sample method: '.htmlspecialchars($method_report['methods'][0], ENT_QUOTES, 'UTF-8');