Content · Web Development · PHP

PHP spl_classes() Function: Usage and Examples

Returns an array of names of SPL classes and interfaces available in the runtime. This is a list of SPL-related declarations, not every class in an application.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Returns an array of names of SPL classes and interfaces available in the runtime.

The examples focus on the operation itself, including the important distinction: get_declared_classes lists loaded classes more broadly.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


spl_classes(): array

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Declared return type: array. Read the behavior notes for important cases such as empty collections and destructive iteration.

Example 1: Basic Use


spl_classes lists SPL class and interface names in the runtime. It is not a complete list of classes in the application.

<?php
$types = spl_classes();
$matches = array_values(array_filter($types, static fn(string $name): bool => str_contains($name, 'Iterator')));
echo implode(", ", array_slice($matches, 0, 5));

Output from a successful local PHP 8.4 run:

AppendIterator, ArrayIterator, CachingIterator, CallbackFilterIterator, DirectoryIterator

Example 2: Checking Behavior


spl_classes lists SPL class and interface names in the runtime. It is not a complete list of classes in the application.

<?php
$types = spl_classes();
$matches = array_values(array_filter($types, static fn(string $name): bool => str_contains($name, 'Spl')));
echo implode(", ", array_slice($matches, 0, 5));

Output from a successful local PHP 8.4 run:

SplDoublyLinkedList, SplFileInfo, SplFileObject, SplFixedArray, SplHeap

Example 3: Practical Example


spl_classes lists SPL class and interface names in the runtime. It is not a complete list of classes in the application.

<?php
$types = spl_classes();
$matches = array_values(array_filter($types, static fn(string $name): bool => str_contains($name, 'Exception')));
echo implode(", ", array_slice($matches, 0, 5));

Output from a successful local PHP 8.4 run:

BadFunctionCallException, BadMethodCallException, DomainException, InvalidArgumentException, LengthException

Important Behavior and Edge Cases


This is a list of SPL-related declarations, not every class in an application.

Comparison with Related Approaches


ApproachDifference
spl_classesReturns an array of names of SPL classes and interfaces available in the runtime.
Related API or alternativeget_declared_classes lists loaded classes more broadly.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsThis is a list of SPL-related declarations, not every class in an application.
Confusing it with a related APIget_declared_classes lists loaded classes more broadly.

Key Takeaways


  • Returns an array of names of SPL classes and interfaces available in the runtime.
  • This is a list of SPL-related declarations, not every class in an application.
  • get_declared_classes lists loaded classes more broadly.

Use spl_classes when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.