Current Location: Home> Latest Articles> class_exists vs interface_exists: Differences and Comparison of PHP Interface and Class Checks

class_exists vs interface_exists: Differences and Comparison of PHP Interface and Class Checks

gitbox 2025-07-02

1. class_exists Function

class_exists is a PHP function used to check whether a specified class has been defined. It returns true if the class exists, and false if not.

Syntax:
<span><span><span class="hljs-title function_ invoke__">class_exists</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$class_name</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$autoload</span></span><span> = </span><span><span class="hljs-literal">true</span></span></span></span>
  • $class_name: The name of the class to check, which must be a string.

  • $autoload: Whether to enable autoloading (default is true). If set to false, PHP will not attempt to autoload the class.

Example:
<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">class_exists</span></span><span>(&#039;MyClass&#039;)) {
    </span><span><span class="hljs-keyword">echo</span></span> &#039;Class MyClass is defined&#039;;
} else {
    </span><span><span class="hljs-keyword">echo</span></span> &#039;Class MyClass is not defined&#039;;
}
</span></span>

In the example above, class_exists checks if MyClass exists. If it does, it outputs "Class MyClass is defined"; otherwise, it outputs "Class MyClass is not defined".

Note that when $autoload is set to true, if the class has not been loaded yet, PHP will attempt to load it. By default, PHP uses autoloading mechanisms (such as spl_autoload_register()) to load classes.


2. interface_exists Function

Similar to class_exists, interface_exists is used to check if an interface has been defined. It returns true or false depending on whether the specified interface exists.

Syntax:
<span><span><span class="hljs-title function_ invoke__">interface_exists</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$interface_name</span></span><span>, </span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-variable">$autoload</span></span><span> = </span><span><span class="hljs-literal">true</span></span></span></span>
  • $interface_name: The name of the interface to check, which must be a string.

  • $autoload: Whether to enable autoloading (default is true). If set to false, PHP will not attempt to autoload the interface.

Example:
<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">interface_exists</span></span><span>(&#039;MyInterface&#039;)) {
    </span><span><span class="hljs-keyword">echo</span></span> &#039;Interface MyInterface is defined&#039;;
} else {
    </span><span><span class="hljs-keyword">echo</span></span> &#039;Interface MyInterface is not defined&#039;;
}
</span></span>

In this example, interface_exists checks if the interface MyInterface is defined. If so, it outputs "Interface MyInterface is defined"; otherwise, it outputs "Interface MyInterface is not defined".


3. Differences Between class_exists and interface_exists

Although class_exists and interface_exists are quite similar, they have fundamental differences:

  • Different Targets:

    • class_exists checks whether a class has been defined.
    • interface_exists checks whether an interface has been defined.
  • Different Uses:

    • Classes usually serve as templates for objects and can be instantiated.
    • Interfaces are contracts that specify methods a class must implement, but they cannot be instantiated.
  • Effect on Autoloading:

    • Both functions support the autoload parameter. If set to true, PHP will attempt to autoload undefined classes or interfaces.
    • The difference is that class autoloading is typically handled by class loaders (such as spl_autoload_register()), whereas interface autoloading is less common since interfaces are not instantiated like classes.

4. Comparison of Use Cases

In practical development, the choice between class_exists and interface_exists depends on different scenarios.

  • Using class_exists:

    • To check whether a class has already been defined or loaded, commonly before instantiation.
    • In an autoloading setup, this function is often used to verify if a class has been loaded to prevent redundant loading or instantiation.

    Example:

    <span><span><span class="hljs-keyword">if</span></span><span> (class_exists(&#039;DatabaseConnection&#039;)) {
        </span><span>$db = new DatabaseConnection();
    } else {
        </span><span>// Handle the case where the class is not defined
    }
    </span></span>
    
  • Using interface_exists:

    • To check if an interface is defined, typically used in polymorphism and dependency injection scenarios.
    • When determining if a class implements a certain interface, you can first use interface_exists to check if the interface exists, then check if the class implements that interface.

    Example:

    <span><span><span class="hljs-keyword">if</span></span><span> (interface_exists(&#039;LoggerInterface&#039;) && $logger instanceof LoggerInterface) {
        </span><span>$logger->log(&#039;Log message&#039;);
    }
    </span></span>