The PHP class_implements API performs a focused operation on an object or value. The examples below show its effect on actual data.
Returns interfaces implemented by a class, including inherited implementations.
What Is class_implements in PHP?
Returns interfaces implemented by a class, including inherited implementations.
Syntax:
class_implements($object_or_class, bool $autoload = ...): array|false
The method accepts the following parameters:
$object_or_class— Required. A PHP parameter named object_or_class; the examples below show a supported value.$autoload— Optional. A bool parameter named autoload; the examples below show a supported value.
Return type declared by PHP: array|false. 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
interface Exportable { public function export(): string; }
class CsvExport implements Exportable { public function export(): string { return "ok"; } }
print_r(class_implements(CsvExport::class));
Output:
Array
(
[Exportable] => Exportable
)
Returns interfaces implemented by a class, including inherited implementations.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
interface Exportable { public function export(): string; }
class CsvExport implements Exportable { public function export(): string { return "ok"; } }
print_r(class_implements(CsvExport::class));
// The output reflects definitions in this PHP process.
Output:
Array
(
[Exportable] => Exportable
)
The return value is an array; check membership with in_array() or isset() as appropriate.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
interface Exportable { public function export(): string; }
class CsvExport implements Exportable { public function export(): string { return "ok"; } }
print_r(class_implements(CsvExport::class));
// Use this result while deciding how to handle optional application features.
Output:
Array
(
[Exportable] => Exportable
)
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- The return value is an array; check membership with in_array() or isset() as appropriate.
- Returns interfaces implemented by a class, including inherited implementations.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
class_implements | Returns interfaces implemented by a class, including inherited implementations. |
| class_parents() | class_implements() lists implemented interfaces; class_parents() lists ancestor classes. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of class_implements | The return value is an array; check membership with in_array() or isset() as appropriate. |
| 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_implements() lists implemented interfaces; class_parents() lists ancestor classes. |
Key Takeaways
- Returns interfaces implemented by a class, including inherited implementations.
- The return value is an array; check membership with in_array() or isset() as appropriate.
- Use the example that matches the values, runtime version and dependencies in your application.
class_implements 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.