Why is it recommended to pass database connection values as function parameters rather than hardcoding them in a class?

It is recommended to pass database connection values as function parameters rather than hardcoding them in a class because it allows for more flexibility and reusability. By passing the connection values as parameters, you can easily switch between different databases or configurations without having to modify the class itself. This also promotes better separation of concerns and makes the code more testable.

function connectToDatabase($host, $username, $password, $database) {
    $conn = new mysqli($host, $username, $password, $database);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}

$host = "localhost";
$username = "root";
$password = "password";
$database = "my_database";

$conn = connectToDatabase($host, $username, $password, $database);