How can namespaces in PHP affect the functionality of a custom autoloader function?

When using namespaces in PHP, the custom autoloader function needs to take into account the namespace structure to correctly load the corresponding classes. By parsing the namespace and class name from the provided class name parameter, the autoloader can dynamically require the appropriate file based on the namespace and class name.

spl_autoload_register(function($class) {
    $prefix = 'Your\\Namespace\\Prefix\\';
    $base_dir = __DIR__ . '/src/';

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }

    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    if (file_exists($file)) {
        require $file;
    }
});