When building tools that inspect PHP code, ReflectionExtension::getClasses() helps answer a focused question: returns ReflectionClass objects for classes supplied by the extension.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionExtension::getClasses()?
Returns ReflectionClass objects for classes supplied by the extension.
Its method signature is:
public function getClasses(): array
This method has no required arguments.
Return value: array
Example 1: Inspect a loaded extension
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
$extensionReflection = new ReflectionExtension("json");
$result = $extensionReflection->getClasses();
echo implode(", ", array_slice(array_map(static fn($class) => $class->getName(), $result), 0, 5));
Output:
JsonSerializable, JsonException
Explanation: Returns ReflectionClass objects for classes supplied by the extension.
Example 2: Compare extension metadata
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
$extensionReflection = new ReflectionExtension("date");
$result = $extensionReflection->getClasses();
echo implode(", ", array_slice(array_map(static fn($class) => $class->getName(), $result), 0, 5));
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
DateTimeInterface, DateTime, DateTimeImmutable, DateTimeZone, DateInterval
Result type: array
Explanation: An extension without exposed classes returns an empty array.
Practical Example: Use extension information for diagnostics
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
$extensionReflection = new ReflectionExtension("standard");
$result = $extensionReflection->getClasses();
echo implode(", ", array_slice(array_map(static fn($class) => $class->getName(), $result), 0, 5));
// Use the reflection result in a simple decision.
if (is_bool($result)) {
echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
echo "\nEntries: " . count($result);
} elseif ($result === null) {
echo "\nNo value returned";
} else {
echo "\nReflection result available";
}
Output:
__PHP_Incomplete_Class, AssertionError, RoundingMode, php_user_filter, StreamBucket
Entries: 6
Explanation: getClassNames returns just their class names as strings.
Important Behavior and Edge Cases
- An extension without exposed classes returns an empty array.
- getClassNames returns just their class names as strings.
- Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionExtension::getClasses() | Returns ReflectionClass objects for classes supplied by the extension. |
Related API | getClassNames returns just their class names as strings. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: An extension without exposed classes returns an empty array. | Handle the specific limitation: An extension without exposed classes returns an empty array. |
| Confusing this method with Related API | getClassNames returns just their class names as strings. |
Key Takeaways
- Returns ReflectionClass objects for classes supplied by the extension.
- An extension without exposed classes returns an empty array.
- getClassNames returns just their class names as strings.
Use ReflectionExtension::getClasses() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.