How can the file_exists() function be used to check for the existence of models or controllers in PHP?

To check for the existence of models or controllers in PHP, we can use the file_exists() function to determine if the file path provided corresponds to an actual file. This can be useful when dynamically loading files based on user input or configuration settings.

// Check if a model file exists
$model_name = 'User';
$model_file = 'models/' . $model_name . '.php';

if (file_exists($model_file)) {
    require_once $model_file;
} else {
    echo "Model file does not exist.";
}

// Check if a controller file exists
$controller_name = 'UserController';
$controller_file = 'controllers/' . $controller_name . '.php';

if (file_exists($controller_file)) {
    require_once $controller_file;
} else {
    echo "Controller file does not exist.";
}