What is the best practice for passing database connection information to PHP functions?

When passing database connection information to PHP functions, it is best practice to use dependency injection. This means passing the database connection object as a parameter to the function that requires it, rather than relying on global variables or constants. This approach makes the code more modular, testable, and easier to maintain.

// Example of passing database connection information using dependency injection

function fetchData(PDO $db) {
    $stmt = $db->query("SELECT * FROM table");
    return $stmt->fetchAll();
}

// Create a PDO connection
$db = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Call the function with the database connection passed as a parameter
$data = fetchData($db);