get_class_methods
get_class_methods(object|string $object_or_class): array
Returns the public method names of a class or object (declaration order); the lightweight entry of the reflection family - a plain list without building a ReflectionClass.
C C23
#include <stdio.h>
/* No reflection: a "method list" is a hand-maintained table of
function pointers paired with their names */
typedef struct Counter Counter;
struct Counter { int n; void (*inc)(Counter *); int (*get)(Counter *); };
static void counter_inc(Counter *c) { c->n++; }
static int counter_get(Counter *c) { return c->n; }
int main(void) {
Counter c = { 0, counter_inc, counter_get };
c.inc(&c);
printf("%d\n", c.get(&c)); /* the "list" is the initializer itself */
return 0;
}
1
C++ C++26
#include <iostream>
#include <string>
#include <vector>
class Counter {
public:
void inc() { ++n; }
int get() const { return n; }
// C++ has no reflection: the "method list" is maintained by hand
static std::vector<std::string> methods() {
return { "inc", "get" };
}
private:
int n = 0;
};
int main() {
Counter c;
c.inc();
std::cout << c.get() << "\n";
for (const auto& m : Counter::methods()) std::cout << m << "\n";
return 0;
}
1 inc get
C# v11.0.100
using System.Linq;
using System.Reflection;
class Counter {
int n;
public void Inc() { n++; }
public int Get() => n;
}
class MainClass {
static void Main() {
// runtime order is NOT guaranteed - sort for deterministic output
var names = typeof(Counter).GetMethods(
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Select(m => m.Name).OrderBy(n => n).ToList();
System.Console.WriteLine(string.Join(",", names));
}
}
Get,Inc
Go v1.27.4
package main
import (
"fmt"
"reflect"
)
type Counter struct{ n int }
func (c *Counter) Inc() { c.n++ }
func (c *Counter) Get() int { return c.n }
func main() {
// reflect lists EXPORTED methods in alphabetical order (documented)
t := reflect.TypeOf(&Counter{})
for i := 0; i < t.NumMethod(); i++ {
fmt.Println(t.Method(i).Name)
}
}
Get Inc
Java v27.0.1
import java.lang.reflect.Method;
import java.util.Arrays;
class Counter {
private int n;
public void inc() { n++; }
public int get() { return n; }
protected void log() { }
}
class Main {
public static void main(String[] args) {
// declared methods only; order is NOT guaranteed - sort it
var names = Arrays.stream(Counter.class.getDeclaredMethods())
.map(Method::getName).sorted().toArray(String[]::new);
System.out.println(String.join(",", names)); // get,inc,log
// the public-only view is the PHP get_class_methods analogue:
var pub = Arrays.stream(Counter.class.getDeclaredMethods())
.filter(m -> java.lang.reflect.Modifier.isPublic(m.getModifiers()))
.map(Method::getName).sorted().toArray(String[]::new);
System.out.println(String.join(",", pub)); // get,inc
}
}
get,inc,log get,inc
JavaScript v26.13.1
class Counter {
#n = 0;
inc() { this.#n++; }
get() { return this.#n; }
#log() {} // private method
}
// own property names of the prototype = declared methods, in order;
// #log lives outside the ordinary property system
console.log(
Object.getOwnPropertyNames(Counter.prototype)
.filter(k => k !== "constructor")
.join(",")
);
inc,get
PHP v8.6.0
class Counter {
private int $n = 0;
public function inc(): void { $this->n++; }
public function get(): int { return $this->n; }
protected function log(): void {} // invisible from outside
}
echo json_encode(get_class_methods("Counter")), "\n"; // ["inc","get"]
$onlyPublic = count(get_class_methods(new Counter())); // object works too
echo $onlyPublic, "\n";
["inc","get"] 2
Python v3.15.2
class Counter:
def __init__(self):
self.n = 0
def inc(self):
self.n += 1
def get(self):
return self.n
def _log(self):
pass
# dir() is sorted alphabetically (documented) and includes inherited
# dunders - filter to the plain declared names
names = [n for n in dir(Counter) if not n.startswith("_")]
print(names)
['get', 'inc']
Rust v1.99.1
// No reflection in Rust: a "method list" is hand-maintained, or
// generated by procedural macros at compile time.
struct Counter {
n: i32,
}
impl Counter {
fn inc(&mut self) { self.n += 1; }
fn get(&self) -> i32 { self.n }
}
fn main() {
let mut c = Counter { n: 0 };
c.inc();
println!("{}", c.get());
println!("inc get"); // the list exists only because we wrote it
}
1 inc get
TypeScript v26.13.1
class Counter {
#n = 0;
inc(): void { this.#n++; }
get(): number { return this.#n; }
#log(): void {} // becomes closure scope, not a prototype property
}
// same runtime answer as plain JS
console.log(
Object.getOwnPropertyNames(Counter.prototype)
.filter(k => k !== "constructor").join(",")
);
inc,get
Language differences
C: no reflection - the "method list" is the hand-written function-pointer table (example); the table must be kept in sync with implementations by hand, a missed entry is a bug.
C++: no reflection (the example hand-writes a static methods()); the only shape until C++26 static reflection lands; a member-function pointer &Counter::inc takes one, not an enumeration.
C#: GetMethods(Public | Instance | DeclaredOnly) (example) - DeclaredOnly excludes inherited methods, runtime order is unspecified and must be sorted; without the filter you also get Object's four instance methods.
Go: reflect.TypeOf(&Counter{}).NumMethod() (example) - alphabetical order is documented; note the POINTER type is required (the pointer method set includes pointer-receiver methods); unexported methods are not listed.
Java: getDeclaredMethods() includes protected/private and has undefined order - sort (the example shows both views); getMethods() is the other view (public + inherited); overloaded methods appear as same-name entries, dedupe by name yourself.
JavaScript: prototype own properties are the methods (example); fields live on instances, not the prototype - the prototype holds only methods and constructor; #private methods are outside the ordinary property system, probe with this.#log in this inside the class.
PHP: declaration order, public only (example) - "lightweight" means no ReflectionClass is built; for protected/private/order details use ReflectionMethod; interface methods are included; an object or a class name both work.
Python: dir() is alphabetical and includes inherited + all dunders (the example filters); for declared-only use vars(Counter) (the __dict__) with a callable filter; attribute access yields bound methods, so a callable(getattr) check is typical.
Rust: no reflection (the example hand-writes the "list" line); methods are not objects, no runtime metadata; procedural macros generating impl-enumeration code at compile time are the standard ecosystem substitute.
TypeScript: runtime identical to JS (example); at compile time keyof typeof Counter.prototype gives partial static power (restricting method-name types since TS 4.x), but #private is never listed.