method_exists
method_exists(object|string $objectOrClass, string $method): bool
Checks whether a method exists on a class or object.
C C23
#include <stdio.h>
/* C has no methods: an ops-table struct simulates a method set,
and "method_exists" is just a NULL check on the slot */
typedef struct {
const char *(*hello)(void);
const char *(*goodbye)(void);
} GreeterOps;
static const char *hello_impl(void) {
return "hi";
}
int main(void) {
GreeterOps ops = { .hello = hello_impl, .goodbye = NULL };
printf("hello: %s\n", ops.hello ? "true" : "false");
printf("goodbye: %s\n", ops.goodbye ? "true" : "false");
return 0;
}
hello: true goodbye: false
C++ C++26
#include <iostream>
#include <string>
#include <type_traits>
struct Greeter {
std::string hello() { return "hi"; }
};
/* compile-time member detection (C++17 idiom): void_t catches the SFINAE miss */
template <typename T, typename = void>
struct has_hello : std::false_type {};
template <typename T>
struct has_hello<T, std::void_t<decltype(std::declval<T&>().hello())>>
: std::true_type {};
template <typename T, typename = void>
struct has_goodbye : std::false_type {};
template <typename T>
struct has_goodbye<T, std::void_t<decltype(std::declval<T&>().goodbye())>>
: std::true_type {};
int main() {
Greeter g;
std::cout << std::boolalpha;
std::cout << "hello: " << has_hello<Greeter>::value << "\n";
std::cout << "goodbye: " << has_goodbye<Greeter>::value << "\n";
std::cout << g.hello() << "\n";
return 0;
}
hello: true goodbye: false hi
C# v11.0.100
using System;
using System.Reflection;
Type t = typeof(Greeter); // MethodInfo lookup via reflection
Console.WriteLine("hello: " + (t.GetMethod("Hello") != null));
Console.WriteLine("goodbye: " + (t.GetMethod("Goodbye") != null));
class Greeter
{
public string Hello() => "hi";
}
hello: True goodbye: False
Go v1.27.4
package main
import "fmt"
type Greeter struct{}
func (Greeter) Hello() string {
return "hi"
}
func main() {
// no method_exists: the method set is fixed at compile time;
// at runtime you can only ask "does it do X" via an interface assertion
var g interface {
Hello() string
} = Greeter{}
fmt.Println(g.Hello())
_, ok := any(Greeter{}).(fmt.Stringer)
fmt.Println("has String():", ok)
}
hi has String(): false
Java v27.0.1
import java.lang.reflect.Method;
public class Main {
static class Greeter {
public String hello() {
return "hi";
}
}
// reflection-based lookup: scan public method names (incl. inherited Object ones)
static boolean hasMethod(Class<?> c, String name) {
for (Method m : c.getMethods()) {
if (m.getName().equals(name)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
Class<Greeter> c = Greeter.class;
System.out.println("hello: " + hasMethod(c, "hello"));
System.out.println("goodbye: " + hasMethod(c, "goodbye"));
}
}
hello: true goodbye: false
JavaScript v26.13.1
class Greeter {
hello() {
return "hi";
}
}
const g = new Greeter();
// typeof check doubles as "exists and is callable"
console.log(typeof g.hello === "function", typeof g.goodbye === "function");
true false
PHP v8.6.0
class Greeter {
public function hello(): string {
return 'hi';
}
}
// method_exists sees declared methods only; __call forwarding needs is_callable
var_dump(method_exists(Greeter::class, 'hello'));
var_dump(method_exists(Greeter::class, 'goodbye'));
bool(true) bool(false)
Python v3.15.2
class Greeter:
def hello(self):
return "hi"
g = Greeter()
# hasattr = attribute exists; callable() narrows it to "invocable"
print(hasattr(g, "hello"), hasattr(g, "goodbye"))
print(callable(getattr(g, "hello", None)))
True False True
Rust v1.99.1
struct Greeter;
impl Greeter {
fn hello(&self) -> String {
String::from("hi")
}
}
fn main() {
// no method_exists: the method set is fixed at compile time —
// calling a missing method is a compile error, not a runtime query
let g = Greeter;
println!("{}", g.hello());
println!("no runtime method check in Rust");
}
hi no runtime method check in Rust
TypeScript v26.13.1
class Greeter {
hello(): string {
return "hi";
}
}
const g = new Greeter();
console.log(typeof g.hello === "function", typeof (g as any).goodbye === "function");
// typeof answers "exists and is callable" in one shot; as any only unblocks the demo
true false
Language differences
PHP: method_exists only sees declared/inherited methods and does not recognize __call magic — for magic forwarding use is_callable (the example's goodbye would still be false via __call); both a class name and an object are accepted.
C: no method system (the example simulates one with an ops-table struct): a NULL check on the member-function-pointer slot is the hand-rolled method_exists — this is how real C OOP styles (glib/GObject) work.
C++: no runtime reflection — method_exists is answered at compile time via template metaprogramming (the example's void_t + decltype idiom); from C++20 a requires expression makes it shorter, but the example keeps the C++17 register to match the build flags.
C#: GetMethod(name) (example) returns a MethodInfo, null when absent; note it searches public methods only — private overloads need BindingFlags.
Go: there is no method_exists — the method set is fixed at compile time and a misspelled method is a compile error; the runtime counterpart is the interface assertion (example): asking "can this value do X" rather than "does this type have a member named X".
Java: reflection's getMethods() (example) scans and compares names; the alternative is getMethod(name) catching NoSuchMethodException; getMethods exposes public only — for any visibility walk up with getDeclaredMethods.
JavaScript: no same-named API; the idiom is typeof obj.method === "function" (example), answering "exists and is callable" in one shot; prototype-chain methods are reachable through instance access.
Python: hasattr (example) is attribute existence, callable() narrows to invocable — together they equal method_exists; hasattr is internally a getattr trial.
Rust: no runtime method query (the example is honest about it): method resolution happens at compile time and trait bounds hold only there; asking "does this runtime value support X" uses dyn Any downcasting or trait objects, but a method-name listing is still out of reach.
TypeScript: same typeof check as JS (example); the difference is strict compile-time guards — as any exists only to demo; production code should make misspelled methods impossible at the type level.