The PHP class_exists API performs a focused operation on an object or value. The examples below show its effect on actual data.
Reports whether a class is defined, optionally triggering autoload.
What Is class_exists in PHP?
Reports whether a class is defined, optionally triggering autoload.
Syntax:
class_exists(string $class, bool $autoload = ...): bool
The method accepts the following parameters:
$class— Required. The class name or object to inspect.$autoload— Optional. A bool parameter named autoload; the examples below show a supported value.
Return type declared by PHP: bool. The value or side effect is demonstrated in the examples below.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
class ReportService {}
var_dump(class_exists(ReportService::class));
Output:
bool(true)
Reports whether a class is defined, optionally triggering autoload.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
class ReportService {}
var_dump(class_exists(ReportService::class));
// The output reflects definitions in this PHP process.
Output:
bool(true)
Check autoload behavior when probing untrusted or optional class names.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
class ReportService {}
var_dump(class_exists(ReportService::class));
// Use this result while deciding how to handle optional application features.
Output:
bool(true)
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Check autoload behavior when probing untrusted or optional class names.
- Reports whether a class is defined, optionally triggering autoload.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
class_exists | Reports whether a class is defined, optionally triggering autoload. |
| interface_exists() | class_exists() checks classes; interface_exists() checks interfaces. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of class_exists | Check autoload behavior when probing untrusted or optional class names. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | class_exists() checks classes; interface_exists() checks interfaces. |
Key Takeaways
- Reports whether a class is defined, optionally triggering autoload.
- Check autoload behavior when probing untrusted or optional class names.
- Use the example that matches the values, runtime version and dependencies in your application.
class_exists is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.