Function spl_classes is Deprecated in PHP 8.6

Function spl_classes is Deprecated in PHP 8.6

In PHP, the spl_classes function returns an array which contains the names of the classes and interfaces provided by the SPL extension. Each key in the array is identical to its value, which makes it possible to check whether a given name belongs to SPL using isset.

Since PHP 8.6, the spl_classes function is deprecated. Consider the following example which counts the names and checks one of them:

<?php

$classes = spl_classes();

echo count($classes).PHP_EOL;
var_dump(isset($classes['ArrayObject']));

Output:

Deprecated: Function spl_classes() is deprecated since 8.6, use ReflectionExtension::getClassNames() instead in main.php on line 3
55
bool(true)

The ReflectionExtension::getClassNames method returns the same names, so it can be used instead:

<?php

$classes = new ReflectionExtension('SPL')->getClassNames();

echo count($classes).PHP_EOL;
var_dump(in_array('ArrayObject', $classes, true));

The method returns a list with numeric keys rather than an array whose keys repeat the values, so a lookup by name uses in_array instead of isset.

Leave a Comment

Cancel reply

Your email address will not be published.