What are some best practices for structuring PHP code to ensure efficient file inclusion and error handling, especially when dealing with dynamic file paths and extensions?

When dealing with dynamic file paths and extensions in PHP, it is important to ensure efficient file inclusion and error handling to prevent vulnerabilities and ensure smooth operation. One best practice is to use absolute paths for file inclusions to avoid any confusion or security risks. Additionally, implementing proper error handling mechanisms such as try-catch blocks can help catch any exceptions that may occur during file inclusion.

try {
    $file_path = '/path/to/directory/' . $filename . '.php';
    if (file_exists($file_path)) {
        require_once $file_path;
    } else {
        throw new Exception('File not found');
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}