Checking if an instance's class implements an interface?

Checking if an instance's class implements an interface?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Checking if an instance's class implements an interface?

Determining whether a specific object instance adheres to a particular interface is a common requirement when writing generic utilities, implementing dependency injection containers, or enforcing architectural contracts at runtime. While you might recall using instanceof for checking concrete classes, PHP does not provide a direct, built-in function like implements_interface() that accepts an object and an interface name as arguments. However, the language provides robust reflection capabilities that allow you to inspect the class hierarchy of an instance dynamically.

The most idiomatic approach involves leveraging the ReflectionClass API. Since an object instance holds a reference to its defining class, you can instantiate a ReflectionClass using the class name derived from the instance. Once you have the reflection object representing the class, you can delegate the check to PHP's built-in implementsInterface() method. This specific function is designed exactly for this purpose: it accepts a ReflectionClass and an interface name (or a string representing the interface) and returns a boolean value.

Here is how you can implement this check in practice:

function checkInterfaceImplementation($instance, $interfaceName): bool {
    if (!is_object($instance)) {
        throw new InvalidArgumentException('The first argument must be an object.');
    }

    // Create a reflection of the instance's class
    $reflection = new ReflectionClass($instance);

    // Check if the class implements the specific interface
    return $reflection->implementsInterface($interfaceName);
}

In this example, we first validate that the input is actually an object to prevent accidental instantiation with a string or null. We then create a ReflectionClass instance pointing to $instance. It is crucial here to note that you do not need to pass the fully qualified class name if you only have the instance; PHP resolves it automatically. The implementsInterface() method will return true if the class directly implements the interface or if any of its parent classes implement it.

This distinction regarding inheritance is a significant advantage over manual string comparison. If you attempted to solve this by inspecting the interface_implements property on the reflection object, you would only see direct implementations and miss those inherited from parents. The implementsInterface() method handles the full class resolution chain for you.

One potential pitfall occurs when dealing with interface names. While implementsInterface() accepts a string, passing an incomplete or typoed interface name will result in a ReflectionException. In production code involving untrusted input, it is wise to verify the interface existence first using interface_exists(). Additionally, if you are working within frameworks like Laravel, this pattern is often necessary for dynamic service resolution where you cannot hard-code specific class types.

If your use case involves checking against multiple interfaces or a collection of classes, looping through them with this reflection logic remains efficient and readable. There is no performance penalty worth worrying about for typical application logic; the overhead of creating a ReflectionClass is negligible compared to the cost of actual method calls or database queries.

For teams working on large-scale PHP applications, understanding these reflection primitives allows you to build flexible dependency injectors without resorting to complex string parsing or external libraries. For more advanced patterns regarding interface resolution in Laravel, resources like https://laravelcompany.com often provide deeper insights into service container internals where this knowledge becomes particularly relevant.

Our network