What are the best practices for utilizing autoloaders in PHP to dynamically load modules and improve code organization?

Autoloaders in PHP can dynamically load classes and improve code organization by automatically including the necessary files when a class is instantiated. To utilize autoloaders effectively, it is recommended to follow PSR-4 standards for autoloading classes, use namespaces to organize classes, and define a custom autoloader function to map class names to file paths.

// Define a custom autoloader function
spl_autoload_register(function ($class) {
    // Convert class name to file path
    $file = __DIR__ . '/' . str_replace('\\', '/', $class) . '.php';
    
    // Check if the file exists and include it
    if (file_exists($file)) {
        include $file;
    }
});

// Example usage of autoloader
$example = new \Namespace\Example();