How can the concept of Dependency Injection be applied to improve the structure of PHP functions?

Issue: PHP functions often have dependencies on external objects or services, making them tightly coupled and difficult to test. By applying the concept of Dependency Injection, we can pass these dependencies as parameters to the function, making it more modular, testable, and easier to maintain. PHP Code Snippet:

// Before using Dependency Injection
function getUserData() {
    $db = new Database();
    $userData = $db->query('SELECT * FROM users');
    return $userData;
}

// After applying Dependency Injection
function getUserData(Database $db) {
    $userData = $db->query('SELECT * FROM users');
    return $userData;
}

// Usage of the function with Dependency Injection
$db = new Database();
$userData = getUserData($db);