interface_exists / trait_exists

interface_exists(string $interface, bool $autoload = true): bool / trait_exists(string $trait, bool $autoload = true): bool

Checks whether an interface or trait is defined (autoload may fire); part of the family with class_exists/enum_exists, a corner of PHP's runtime type table.

C C23

C23
#include <stdio.h>

/* C has no interface keyword - the contract is a struct of function
   pointers (a vtable); "implementing" means filling the table */
typedef struct {
    const char *(*kind)(void);
} Drawable;

static const char *circle_kind(void) { return "circle"; }

int main(void) {
    Drawable d = { circle_kind }; /* the table IS the interface */
    printf("%d\n", d.kind != NULL); /* contract satisfied at init */
    printf("%s\n", d.kind());
    return 0;
}
Output
1
circle

C++ C++26

C++26
#include <iostream>

class Drawable { // interface idiom: pure virtual, no state, virtual dtor
public:
    virtual ~Drawable() = default;
    virtual const char* kind() const = 0;
};

class Circle : public Drawable {
public:
    const char* kind() const override { return "circle"; }
};

int main() {
    Drawable* d = new Circle();
    // runtime "interface check" = does this object satisfy the contract
    std::cout << (d != nullptr) << "\n";
    std::cout << d->kind() << "\n";
    delete d;
    return 0;
}
Output
1
circle

C# v11.0.100

v11.0.100
using System;

interface Drawable { string Kind(); }

class Circle : Drawable {
    public string Kind() => "circle";
}

class MainClass {
    static void Main() {
        // explicit-interface camp: reflection carries an IsInterface flag
        Console.WriteLine(typeof(IDisposable).IsInterface);
        Console.WriteLine(typeof(string).IsInterface);
        // by-name probe across loaded assemblies (interface_exists analogue)
        Type t = Type.GetType("System.IDisposable");
        Console.WriteLine(t != null && t.IsInterface);
        Console.WriteLine(new Circle() is Drawable);
    }
}
Output
True
False
True
True

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go is the implicit camp: interfaces are satisfied structurally, with NO
// declaration-site registration and therefore no "exists" function
type Drawable interface{ Kind() string }

type Circle struct{}

func (Circle) Kind() string { return "circle" }

func main() {
	var d Drawable = Circle{} // the assignment itself is the proof
	fmt.Println(d.Kind())
	// a failed interface conversion is the only runtime "satisfies" probe
	if _, ok := any(Circle{}).(Drawable); ok {
		fmt.Println("satisfies")
	}
}
Output
circle
satisfies

Java v27.0.1

v27.0.1
interface Drawable { // implicitly static and abstract
    String kind();
}

class Circle implements Drawable {
    public String kind() { return "circle"; }
}

class Main {
    public static void main(String[] args) throws Exception {
        // explicit-interface camp: Class carries an isInterface flag
        System.out.println(Drawable.class.isInterface());
        System.out.println(String.class.isInterface());
        // by-name probe across the classpath (interface_exists analogue)
        Class<?> c = Class.forName("java.lang.Runnable");
        System.out.println(c.isInterface());
    }
}
Output
true
false
true

JavaScript v26.13.1

v26.13.1
// JS has no interface concept at runtime - duck typing replaces it:
// an object "implements" whatever methods it happens to have
const circle = {
    kind() { return "circle"; },
};

// structural probe: no registry, no metadata, just the object itself
console.log(typeof circle.kind === "function");
console.log("implements" in circle); // no interface metadata to query
Output
true
false

PHP v8.6.0

v8.6.0
// existence probes for the interface / trait corners of the type table
interface Drawable {}
trait Timestamped {}

var_dump(interface_exists('Drawable'));      // defined interface
var_dump(interface_exists('Timestamped'));   // traits are NOT interfaces
var_dump(trait_exists('Timestamped'));       // defined trait
var_dump(trait_exists('Drawable'));          // interfaces are NOT traits
var_dump(interface_exists('Traversable'));   // built-in interface
var_dump(interface_exists('NoSuchIface', false)); // autoload disabled
// class_alias works for interfaces and traits too (family with class_exists)
Output
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)

Python v3.15.2

v3.15.2
from abc import ABC, abstractmethod
import inspect

# ABC keeps a registry of (virtual) subclasses - the closest thing to
# interface_exists in Python's implicit-implementation culture
class Drawable(ABC):
    @abstractmethod
    def kind(self):
        ...


class Circle(Drawable):
    def kind(self):
        return "circle"


print(issubclass(Circle, Drawable))
print(len(Drawable.__subclasses__()) >= 1)  # the registry is queryable
print(inspect.isabstract(Drawable))
Output
True
True
True

Rust v1.99.1

v1.99.1
// Rust traits are COMPILE-time: no registry, no exists probe -
// an impl block is checked statically and then forgotten
trait Drawable {
    fn kind(&self) -> &'static str;
}

struct Circle;
impl Drawable for Circle {
    fn kind(&self) -> &'static str { "circle" }
}

fn render(d: &dyn Drawable) {
    println!("{}", d.kind());
}

fn main() {
    render(&Circle); // satisfaction was proven at compile time
}
Output
circle

TypeScript v26.13.1

v26.13.1
// TS interfaces exist only at compile time - they are erased, so
// there is no runtime interface_exists; the type checker is the probe
interface Drawable {
    kind(): string;
}

class Circle implements Drawable {
    kind() { return "circle"; }
}

const c: Drawable = new Circle(); // assignment checked at compile time
console.log(typeof c.kind === "function"); // runtime only sees methods
Output
true

Language differences

C: no interface keyword - the contract is a function-pointer struct (vtable, example); "existence" means the table is filled, established at init.

C++: the interface idiom is a pure-virtual base class (example); C++20 concepts move interface constraints into compile-time template metaprogramming; runtime can only ask "can this object convert to that interface pointer".

C#: explicit-interface camp - typeof(X).IsInterface (example); by-name probing via Type.GetType; isomorphic to Java's isInterface. No trait counterpart (extension methods are a distant mixin approximation).

Go: the IMPLICIT camp's flagship - interfaces are satisfied structurally by the method set, with no declaration-site registration and therefore no exists concept (example: assignment and type assertion are the only probes); interface{}/any is the empty interface.

Java: explicit camp - X.class.isInterface() (example); Class.forName for by-name probes; no trait counterpart.

JavaScript: no interfaces at runtime - duck typing (example); Symbol protocols are the only "structural contract" approximation.

PHP: closes the family with class_exists/enum_exists - interface_exists/trait_exists share the autoload second parameter; interfaces and traits are mutually exclusive categories (the middle four lines of the example); a trait is a compile-time mixin flattened into the class with no standalone runtime entity - which is exactly why trait_exists exists (probing the DEFINITION); class_alias works for interfaces and traits too. Teaching point "explicit vs implicit camps": PHP/Java/C#/C++ are the explicit-declaration camp; Go/Rust/TS/JS the implicit-structural camp.

Python: the ABC registry is queryable (__subclasses__, example); isinstance/issubclass signal explicitness in duck-typing culture; abc.register can virtually register unrelated classes.

Rust: traits are checked at compile time and then forgotten (example); no runtime registry; dyn Trait satisfaction is guaranteed by the compiler.

TypeScript: interfaces are erased at compile time (example) - implements violations are compile errors, leaving no runtime trace.