What are the best practices for setting up an autoloader in PHP to ensure cross-OS compatibility, especially when developing on Windows and deploying on Linux?

When setting up an autoloader in PHP for cross-OS compatibility, it's important to use the DIRECTORY_SEPARATOR constant instead of hardcoding directory separators. This ensures that the autoloader will work correctly on both Windows and Linux systems. Additionally, using the realpath function can help resolve any path issues that may arise due to differences in file system handling between the two operating systems.

spl_autoload_register(function($class) {
    $file = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
    $path = realpath(__DIR__ . DIRECTORY_SEPARATOR . $file);
    
    if (file_exists($path)) {
        require $path;
    }
});